Get a UTC timestamp [duplicate] - javascript

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

Related

Creating a JavaScript Date object from days since January 1st, 1970 [duplicate]

This question already has answers here:
Is the Javascript date object always one day off?
(29 answers)
Closed 2 years ago.
I have found many examples on the internet of converting from seconds or milliseconds to a JavaScript Date, but only a few for converting from days to a Date object. When I tried these examples in my own code (with my own values), I was unable to replicate their results.
My API returns an integer value representing days since UNIX epoch. I need to find a way to convert this to a JavaScript date object, so that I can display it in a human readable format.
For example:
new Date(18521 * 86400 * 1000)
// multiply by 86400 (seconds in a day), then by 1000 to convert to milliseconds.
At the time of writing, the date is 9/17/2020 MMDDYYYY, and 18522 days have passed since UNIX epoch. However, when I try to retrieve yesterday's UNIX date 18521 using the date constructor with some math (mentioned above), I get the incorrect date: Sep 15 2020. I would expect to get (Sep 16 2020) since I have only subtracted one day, but for some reason that is not the case.
Is there anything I am doing incorrectly here? What should I change to make my code work?
The way you're doing it is correct. There are always 8.64e7 ms in an ECMAScript UTC day and ECMASCript and Java have the same epoch. The way you're doing it sets the UTC date to the correct value (which is the right way to do it), not the local date. The default toString shows the local date so if the host is set to a negative offset, it will show one day earlier.
So get the UTC date instead:
new Date(18521 * 8.64e7).toISOString() //2020-09-16T00:00:00.000Z.
If you want to do this with a local date, then create a date for Jan 1970 and set the "date" parameter to the day count plus 1 (because January starts on 1 not 0):
console.log('UTC : ' + new Date(18521 * 8.64e7).toISOString() +
'\nLocal: ' + new Date(1970, 0, 18521 + 1).toDateString());

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

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());
})

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);

How to get timestamp value of only date [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 5 years ago.
I am new to Node Js
I am using Date.now() to get the timestamp of current time
But i wanted the timestamp value of only current date not including the time.
For Example
Now current time is August 9, 2017 8:19:28 PM and the timestamp will be 1502309968000
But I want timestamp of only August 9, 2017 12:00:00 AM and the timestamp will be 1502236800000
How to solve this ?
Create a date object, and set hours, minutes, seconds and milliseconds to zero
var date = new Date();
date.setHours(0,0,0,0);
var time = date.getTime();
console.log(time);
Note that setHours accepts minutes, seconds etc. as well.
.setHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])
just set the Hours to 0
d = new Date();
d.setHours(0,0,0,0);
Try http://momentjs.com/
It will give you the format that you needed.
enter image description here

Categories