Convert DateTime format to another DateTime format in Lua

I have a question where a date value is used to send to the application for some processing, and it needs to be formatted into a different DateTime format first.

Yes) I have

I start with the DateTime value in the following format:

> MM / DD / YYYY hh: mm: ss [AM / PM]

< p>What I need

To use Lua in a certain way I need to convert it to DateTime format:

> YYYY-MM-DD hh:mm:ss

< p>I have tried many different things and I have no luck; I will post what I have tried, I think this may be the most appropriate but maybe this is also closed, and there is a simpler or simpler LUA method.

What did I try

print(os.date("%Y-%m-%d %H:%M:%S", "05/17/2017 05:17:00 PM"))

Error

stdin:1: bad argument #2 to ‘ date'(number expected, got string)

What do I assume

I think this means that the data type is a string instead of a datetime type, it It needs to be converted before os.date can use it. I also assume that I can pass the value of the datetime string to os.date, and it will output the same datetime value wisely, but I need to use the new format. /p>

I have read various posts about the need to parse the date-time value part, and then use these parsed parts to build the format you need. I hope someone can help me guide me in the right direction with this task , So I don’t need to run through more trials and errors and do more reading, but I will do that until I hear it back or give up one day.

A brief review

>My value starts at: 05/17/2017 05:17:00 PM
>I want to send this value to 2017-05-17 17:17:00

Use the new format I can use it to process the application.

Other

Any level of Lua logic to solve this problem will be explored, whether it is a custom function, Different built-in functions to get the date and time format and so on. I am open to all ideas, only print() and os.date (functions in my example, to keep things simple, but I am open to more complex things.

Unlike other languages, Lua does not have a DateTime type; dates and times are represented in Lua by strings, numbers and strings and number tables. This way The result is that there are many ways to use dates in Lua. The best way depends on your situation.

The error you get from os.date is because the second parameter should be A number in Unix time (the number of seconds since January 1, 1970). In order to work in your case, you need to use the os.time function to convert the timestamp to Unix time. However, os.time requires one The number table is used as input, so you need to do the actual date parsing and find the number from the date string.

However, in your case, your input is a string, and you want the output Is a similar string, so you only need to do date parsing instead of using os.date and os.time to convert the results. This is what @tonypdmtr did with string.gsub in his answer. The following is using string. match and string.format do something very similar:

local date = '05/17/2017 05:17:00 PM'
local month, day , year, hours, minutes, seconds, amPm = date:match('^(%d%d)/(%d%d)/(%d%d%d%d) (%d%d):(% d%d):(%d%d) ([AP]M)$')
if not month then
- Our entire match failed, and no captures were made
error( 'could not parse date "'.. date ..'"')
end
if amPm =='PM' then
hours = string.format('%2d', tonumber( hours) + 12)
end
local newDate = string .format(
'%s-%s-%s %s:%s:%s',
year, month, day, hours, minutes, seconds
)
print(newDate) - 2017-05-17 17:17:00

I have a problem where a date value is used to send to the application for some processing, which requires First format it into a different DateTime format.

Yes) I have

I start with the DateTime value in the following format:

> MM / DD / YYYY hh: mm: ss [AM / PM]

What I need

To use Lua in a certain way I need to convert it to DateTime format:

> YYYY-MM-DD hh:mm:ss

I have tried many different things, I have no luck; I will post what I have tried, I think this may be the most appropriate But maybe this is also off, and there is a simpler or simpler LUA method.

What did I try

print(os.date ("%Y-%m-%d %H:%M:%S","05/17/2017 05:17:00 PM"))

error

stdin:1: bad argument #2 to’date'(number expected, got string)

What am I assuming

< p>I think this means that the data type is a string instead of a datetime type, and it needs to be converted before os.date can use it. I also assume that I can pass the value of the datetime string to os.date, And it will output the same datetime value wisely, but I need to use the new format.

I have read various posts about the need to parse the datetime value part, and then use these parsed parts to Build the format you need. I hope someone can help guide me in the right direction with this task, so I don’t need to run through more trial and error and do more reading, but I will do it until I hear Come back or give up one day.

A brief review

>My value starts at: 05/17/2017 05:17:00 PM
>I want to send this value to 2017-05-17 17:17 :00

Using the newly formatted date and time, I can use it to process the application.

Other

Any level of Lua logic can solve this problem Will be explored, whether it is a custom function, different built-in functions to get the date time format, etc. I am open to all ideas, only print() and os.date (functions in my example) are shown , To keep it simple, but I am open to more complicated things.

Unlike other languages, Lua does not have a DateTime type; dates and times pass strings, numbers And strings and number tables are represented in Lua. The result of this is that there are many ways to use dates in Lua, and the best way depends on your situation.

You get from os. The error you get for date is because the second parameter should be a number in Unix time (the number of seconds since January 1, 1970). In order to work in your case, you need to use the os.time function to set the timestamp Converted to Unix time. However, os.time requires a table of numbers as input, so you need to do the actual date parsing and find the number from the date string.

But, in your case, you The input is a string, and the output you want is a similar string, so you only need to perform date parsing instead of using os.date and os.time to convert the results. This is what @tonypdmtr said in his answer What string.gsub does. The following is using string.match and string.format to do something very similar:

local date = '05/17/2017 05:17 :00 PM'
local month, day, year, hours, minutes, seconds, amPm = date:match('^(%d%d)/(%d%d)/(%d%d%d %d) (%d%d):(%d%d):(%d%d) ([AP]M)$')
if not month then
- Our entire match failed , and no captures were made
error('could not parse date "'.. date ..'"')
end
if amPm =='PM' then
hours = string.format('%2d', tonum ber(hours) + 12)
end
local newDate = string.format(
'%s-%s-%s %s:%s:%s',
year, month, day, hours, minutes, seconds
)
print(newDate) - 2017-05-17 17:17:00

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3271 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.