Converting Human-Friendly Date to Milliseconds

Converting human-friendly date to milliseconds

strtotime($human_readable_date) * 1000

Converting human readable dates to milliseconds and back again without losing information

Remove period prior to %F and it works. See example here: http://www.boost.org/doc/libs/1_60_0/doc/html/date_time/date_time_io.html#time_input_facet_accessors

Note also that milliseconds are not enough to store the whole precision from your example 2016-04-14T07:47:50.120043Z. Since you have 6 decimal digits, you would need microseconds.

Also note that there are a huge load of microseconds since 01-Jan-1970, so long is not enough. And it is not a good idea to use long anyway, since this type may have different size on different platforms. A better idea would be to use uint64_t for this purpose, that's what boost is using to store time durations. With help of 64 bits you will be able to store microseconds for another half of million of years or so.

how to convert epoch time to human readable time in milliseconds

a = 1517166673385
paste0(format(as.POSIXct(a/1000,origin="1970-01-01", tz="GMT"),"%Y%m%d%H%M%S"),sprintf("%03d",a%%1000))

[1] "20180128191113385"
in a function form:

fun=function(a){
paste0(format(as.POSIXct(a/1000,origin="1970-01-01",
tz="GMT"),"%Y%m%d%H%M%S"),sprintf("%03d",a%%1000))
}

d=c(1517166673385, 1517701556075)

fun(d)
[1] "20180128191113385" "20180203234556075"

How to convert milliseconds into human readable form?

Well, since nobody else has stepped up, I'll write the easy code to do this:

x = ms / 1000
seconds = x % 60
x /= 60
minutes = x % 60
x /= 60
hours = x % 24
x /= 24
days = x

I'm just glad you stopped at days and didn't ask for months. :)

Note that in the above, it is assumed that / represents truncating integer division. If you use this code in a language where / represents floating point division, you will need to manually truncate the results of the division as needed.

Convert python datetime to timestamp in milliseconds

In Python 3 this can be done in 2 steps:

  1. Convert timestring to datetime object
  2. Multiply the timestamp of the datetime object by 1000 to convert it to milliseconds.

For example like this:

from datetime import datetime

dt_obj = datetime.strptime('20.12.2016 09:38:42,76',
'%d.%m.%Y %H:%M:%S,%f')
millisec = dt_obj.timestamp() * 1000

print(millisec)

Output:

1482223122760.0

strptime accepts your timestring and a format string as input. The timestring (first argument) specifies what you actually want to convert to a datetime object. The format string (second argument) specifies the actual format of the string that you have passed.

Here is the explanation of the format specifiers from the official documentation:

  • %d - Day of the month as a zero-padded decimal number.
  • %m - Month as a zero-padded decimal number.
  • %Y - Year with century as a decimal number
  • %H - Hour (24-hour clock) as a zero-padded decimal number.
  • %M - Minute as a zero-padded decimal number.
  • %S - Second as a zero-padded decimal number.
  • %f - Microsecond as a decimal number, zero-padded to 6 digits.

How do I convert human readable hours into milliseconds using javascript?

I dont think there is any built-in function for this, however it would not be complicated to achieve what you want.

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date

First, create a date with 0 elapsed milliseconds:

var d = new Date(0);

Parse your time string and set the hours of the date accordingly:

var t = timeString.match(/(\d{1,2}):(\d{2}) ([AP]M)/),
h = parseInt(t[1], 10),
isAm = t[3] === 'AM',
isMidnight = h === 12 && isAm,
isNoon = h === 12 && !isAm;

d.setUTCHours(isMidnight? 0 : h + (isAm || isNoon? 0 : 12), parseInt(t[2], 10));

Use the getTime function to get the milliseconds:

console.log(d.getTime()); //10800000

Here's a re-usable function:

function getTimeMilliseconds(timeString) {
var t = timeString.match(/(\d{1,2}):(\d{2}) ([AP]M)/),
h = parseInt(t[1], 10),
isAm = t[3] === 'AM',
isMidnight = h === 12 && isAm,
isNoon = h === 12 && !isAm;

return new Date(0).setUTCHours(isMidnight? 0 : h + (isAm || isNoon? 0 : 12), parseInt(t[2], 10));
}

getTimeMilliseconds('3:00 AM');

Note that you don't really need the Date object to achieve this, but I use it because it does the math for you, however you could always do hours * 3600000 + minutes * 60000.

function getTimeMilliseconds(timeString) {
var t = timeString.match(/(\d{1,2}):(\d{2}) ([AP]M)/),
h = parseInt(t[1], 10),
isAm = t[3] === 'AM',
isMidnight = h === 12 && isAm,
isNoon = h === 12 && !isAm;

return (isMidnight? 0 : h + (isAm || isNoon? 0 : 12)) * 3600000 + parseInt(t[2], 10) * 60000;
}

Converting epoch timestamp into a readable date

That epoch is in seconds, you need to multiply it by 1000.



Related Topics



Leave a reply



Submit