Hello friends.
I need help to make this date " 2017-08-06T04:51:46.519Z" in the string format for the date and time of the device.
Can someone help me?
Thanks
Hello friends.
I need help to make this date " 2017-08-06T04:51:46.519Z" in the string format for the date and time of the device.
Can someone help me?
Thanks
Maybe this tutorial? https://docs.coronalabs.com/tutorial/data/timeDates/index.html
There isn’t a “date time of the device” standard format that I’m aware of. Can you provide a better example of what you want to convert the date to?
Rob
This datetime comes from my server in UTC format.
I want to display it in device timezone format.
In my case it’s UTC / GMT -3 hour.
function convertDate(dateString) local pattern = '(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)%.(.-)Z' local xyear, xmonth, xday, xhour, xminute, xseconds, offset = dateString:match(pattern) local convertedTimestamp = os.time({ year = xyear, month = xmonth, day = xday, hour = xhour, min = xminute, sec = xseconds }) local dt = os.date('\*t', convertedTimestamp) dt.hour = dt.hour - 3 return os.date('%Y-%m-%d %H:%M:%S', os.time(dt)) end print(convertDate('2017-08-06T18:58:44.955Z'))
I want to make this more dynamic for other timezones
I would not add/subtract a value to dt.hour. Because dt.hour == -3 or dt.hour == 27 doesn’t make any sense and I don’t know how it will be converted. What I would do is use: os.date("%z") to get the Timezone offset for your location and then parse out the hours and minutes from that (world wide there are timezones that are 30 min off from the rest of the world).
Maybe do this:
local offsetSign, offsetHours, offsetMinutes = os.date("%z"):match( "([%-%+])(%d%d)(%d%d)" ) p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff} span.s1 {font-variant-ligatures: no-common-ligatures}local tzOffsetDirection = 1 if offsetSign == "-" then tzOffsetDirection = -1 end convertedTimestamp = convertedTimestamp + ( offsetHours \* 3600 + offsetMinutes \* 60 ) \* tzOffsetDirection return os.date('%Y-%m-%d %H:%M:%S', os.time( convertedTimestamp ) )
Or something similar to that.
Rob
Great!
The final code:
function convertDate(dateString) local pattern = '(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)%.(.-)Z' local xyear, xmonth, xday, xhour, xminute, xseconds, offset = dateString:match(pattern) local convertedTimestamp = os.time({ year = xyear, month = xmonth, day = xday, hour = xhour, min = xminute, sec = xseconds }) local offsetSign, offsetHours, offsetMinutes = os.date('%z'):match('([%-%+])(%d%d)(%d%d)') local tzOffsetDirection = 1 if offsetSign == '-' then tzOffsetDirection = -1 end convertedTimestamp = convertedTimestamp + ( offsetHours \* 3600 + offsetMinutes \* 60 ) \* tzOffsetDirection return os.date('%Y-%m-%d %H:%M:%S', convertedTimestamp) end
Maybe this tutorial? https://docs.coronalabs.com/tutorial/data/timeDates/index.html
There isn’t a “date time of the device” standard format that I’m aware of. Can you provide a better example of what you want to convert the date to?
Rob
This datetime comes from my server in UTC format.
I want to display it in device timezone format.
In my case it’s UTC / GMT -3 hour.
function convertDate(dateString) local pattern = '(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)%.(.-)Z' local xyear, xmonth, xday, xhour, xminute, xseconds, offset = dateString:match(pattern) local convertedTimestamp = os.time({ year = xyear, month = xmonth, day = xday, hour = xhour, min = xminute, sec = xseconds }) local dt = os.date('\*t', convertedTimestamp) dt.hour = dt.hour - 3 return os.date('%Y-%m-%d %H:%M:%S', os.time(dt)) end print(convertDate('2017-08-06T18:58:44.955Z'))
I want to make this more dynamic for other timezones
I would not add/subtract a value to dt.hour. Because dt.hour == -3 or dt.hour == 27 doesn’t make any sense and I don’t know how it will be converted. What I would do is use: os.date("%z") to get the Timezone offset for your location and then parse out the hours and minutes from that (world wide there are timezones that are 30 min off from the rest of the world).
Maybe do this:
local offsetSign, offsetHours, offsetMinutes = os.date("%z"):match( "([%-%+])(%d%d)(%d%d)" ) p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff} span.s1 {font-variant-ligatures: no-common-ligatures}local tzOffsetDirection = 1 if offsetSign == "-" then tzOffsetDirection = -1 end convertedTimestamp = convertedTimestamp + ( offsetHours \* 3600 + offsetMinutes \* 60 ) \* tzOffsetDirection return os.date('%Y-%m-%d %H:%M:%S', os.time( convertedTimestamp ) )
Or something similar to that.
Rob
Great!
The final code:
function convertDate(dateString) local pattern = '(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)%.(.-)Z' local xyear, xmonth, xday, xhour, xminute, xseconds, offset = dateString:match(pattern) local convertedTimestamp = os.time({ year = xyear, month = xmonth, day = xday, hour = xhour, min = xminute, sec = xseconds }) local offsetSign, offsetHours, offsetMinutes = os.date('%z'):match('([%-%+])(%d%d)(%d%d)') local tzOffsetDirection = 1 if offsetSign == '-' then tzOffsetDirection = -1 end convertedTimestamp = convertedTimestamp + ( offsetHours \* 3600 + offsetMinutes \* 60 ) \* tzOffsetDirection return os.date('%Y-%m-%d %H:%M:%S', convertedTimestamp) end