gave same time on converting Date.now() [duplicate] - javascript

This question already has answers here:
Convert a Unix timestamp to time in JavaScript
(34 answers)
Closed 2 years ago.
I'm looking for something to convert exact time
var arr = [1585287883,1585287876,1585287736,1585287730,1585287725,1585287720];
arr.forEach(val=>{
console.log(Date(val).toString())
})

There are 2 issues with what you posted:
Date -> new Date
The timestamps in javascript should represent milliseconds, currently they represent seconds. You can multiply the timestamps by 1000 when building the date object, to convert seconds to milliseconds.
var arr = [1585287883,1585287876,1585287736,1585287730,1585287725,1585287720];
arr.forEach(val=>{
console.log(new Date(val*1000).toString())
})

The fact is that you're using the wrong input format. Instead of seconds since 1970-01-01, you should use milliseconds.
A JavaScript date is fundamentally specified as the number of
milliseconds that have elapsed since midnight on January 1, 1970, UTC.
This date and time is the same as the UNIX epoch, which is the
predominant base value for computer-recorded date and time values.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Simply, multiply you're input by 1000 (and use the keyword new ;) )
var arr = [1585287883,1585287876,1585287736,1585287730,1585287725,1585287720];
arr.forEach(val=>{
console.log(new Date(val * 1000).toString());
})

Related

JavaScript Elapsed time since a previous date/time [duplicate]

This question already has answers here:
How to calculate date difference in JavaScript? [duplicate]
(24 answers)
Closed 4 years ago.
I have been trying to calculate the exact time since a very specific date in history using Javascript.
The Date is Feb 24th 2008 17:30 GMT+0
I need help in calculating exact time passed down to the second using Javascript.
Here is the previous date and the current date.
I need help in calculating Hours, Minutes and Seconds since that date/time.
var previousDate = new Date("Sun Feb 24 2008 17:30:00 GMT+0");
var currentDate = new Date();
It's easy to calculate the milliseconds between two dates:
var millis = currentDate - previousDate;
From there you can calculate the seconds:
var seconds = Math.round(millis / 1000);
Calculation of minutes, hours, ... is straightforward (division by 60 or 60*60).
Parsing of date strings in javascript fraught. If you have a specific date, far better to avoid the built–in parser. If it's UTC, use Date.UTC to generate the time value.
Then just subtract from any other date to get the difference in milliseconds and convert to seconds, as hgoebi suggests.
var epoch = new Date(Date.UTC(2008,1,24,17,30));
console.log(epoch.toISOString());
console.log(`Seconds from epoch to now: ${(Date.now() - epoch)/1000|0}`);

Convert UTC date seconds to Date [duplicate]

This question already has answers here:
Convert a Unix timestamp to time in JavaScript
(34 answers)
Closed 5 years ago.
Pretty straightforward issue, but I haven't found any information on this after looking around a bunch.
Essentially, I want to convert a series of UTC dates (e.g. "1505952000") into regular date strings (e.g., "9/21"), to use today as an example.
For some reason, however, .toDateString() is erroring out as "not a function" when I try to run it. How do I make this simple conversion work?
Here's my code, and I've console-logged day.dt to ensure that it's a valid UTC date when it runs:
let dt = day.dt.toDateString();
UTC var stored in seconds from Jan. 1, 1970.
So to convert it back to the local date time, use this snippet:
var d = new Date(0);
d.setUTCSeconds(1505952000);
console.log(d);
OR
var d = new Date(1505952000 * 1000); // Because this constructor takes miliseconds.
console.log(d);

Equivalent code in python (time) [duplicate]

This question already has answers here:
Converting unix timestamp string to readable date
(19 answers)
Closed 6 years ago.
Javascript code:
var date = new Date(1466278504960);
return: Sat Jun 18 2016 20:35:04 GMT+0100 (WEST)
How can I convert the same number to date in python ?
When I use
datetime.datetime.fromtimestamp(int("1466278504960")).strftime('%Y-%m-%d %H:%M:%S'))
I receive this error: ValueError: year is out of range
datetime.datetime.fromtimestamp will do this, but you need to divide the value by 1000 first (the numeric value you give and JavaScript's Date expects is in milliseconds since the epoch, where Python's API takes a floating point seconds since the epoch):
from datetime import datetime
date = datetime.fromtimestamp(1466278504960 / 1000.)
That makes the raw datetime object; if you want it formatted the same, you should take a look at datetime object's strftime method.
It's almost the same. You just have to convert the units.
Date from javascript specifies the number in milliseconds, in other words, expects a number in millisenconds as a parameter. When the python date takes seconds.

Javascript: Finding the difference between a datetime object and current time in seconds [duplicate]

This question already has answers here:
Get time difference between two dates in seconds
(10 answers)
Closed 7 years ago.
Lets say I have a date-time object 2015-12-31T12:59. Is there a way in JavaScript to find the difference between current date-time and the above date-time object in seconds? Basically, is there a way to find out the time in seconds from this very moment till the date-time specified by a future date-time object?
I did some digging but couldn't find anything that could be of any use for me in this case.
To achieve this you can simply subtract the date object from Date.now(). Then take the millisecond value that it gives you, and divide by 1000 to get the second value. Here is a live example:
var date1 = new Date("2015-12-31T12:59");
var date2 = Date.now();
document.getElementById("output").innerHTML = (date1 - date2) / 1000;
Seconds between now and (2015-12-31T12:59): <span id="output"></span>

Get a UTC timestamp [duplicate]

This question already has answers here:
How do I get a UTC Timestamp in JavaScript?
(16 answers)
Closed 10 years ago.
How can I get the current UTC timestamp in JavaScript? I want to do this so I can send timestamps from the client-side that are independent of their timezone.
new Date().getTime();
For more information, see #James McMahon's answer.
As wizzard pointed out, the correct method is,
new Date().getTime();
or under Javascript 1.5, just
Date.now();
From the documentation,
The value returned by the getTime method is the number of milliseconds
since 1 January 1970 00:00:00 UTC.
If you wanted to make a time stamp without milliseconds you can use,
Math.floor(Date.now() / 1000);
I wanted to make this an answer so the correct method is more visible.
You can compare ExpExc's and Narendra Yadala's results to the method above at http://jsfiddle.net/JamesFM/bxEJd/, and verify with http://www.unixtimestamp.com/ or by running date +%s on a Unix terminal.
You can use Date.UTC method to get the time stamp at the UTC timezone.
Usage:
var now = new Date;
var utc_timestamp = Date.UTC(now.getUTCFullYear(),now.getUTCMonth(), now.getUTCDate() ,
now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds());
Live demo here http://jsfiddle.net/naryad/uU7FH/1/
"... that are independent of their timezone"
var timezone = d.getTimezoneOffset() // difference in minutes from GMT

Categories