I read the documentation for the Date.now() method.
It's understood what it returns, and my question focus on, literally, the format name.
I have a REST call, where on of its properties is expirtyDate, and I wish to format to be as returned by the Date.now() method (i.e- the numbers of milliseconds passed from Jan 1970).
But how can I describe this format the my teammates? I can't tell them "use the Date.now() of JS". I prefer to ask them for "use time format"
So how should I call this format?
From the documentation you link to:
the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC
or
the milliseconds elapsed since the UNIX epoch
AKA UNIX time, POSIX time, and epoch time).
Related
I am trying to get an UTC Date using the moment.js library (just to send it to my server), as follows:
const eighteenYearsAgoUTC = moment().utc().subtract(18, "years").toDate();
const eighteenYearsAgoUTCSinceUnixEpoch = eighteenYearsAgoUTC.valueOf()
console.log(eighteenYearsAgoUTCSinceUnixEpoch);
The millis since unix epoch are: 1051875596343
But... if I do the same without utc, I get the same result
const eighteenYearsAgoUTC = moment().subtract(18, "years").toDate();
const eighteenYearsAgoUTCSinceUnixEpoch = eighteenYearsAgoUTC.valueOf()
console.log(eighteenYearsAgoUTCSinceUnixEpoch);
1051875596343
Why am I getting the same milliseconds since Unix Epoch for an UTC date and a local Date?
My local date is: Fri May 02 2003 13:37:00 GMT+0200 (Central Europe)
The utc method just changes how moment parses and formats dates. The underlying information is still the same.
Milliseconds-since-The-Epoch values are always UTC. Both of your code snippets do the same thing:
Get "now"
Subtract 18 years
Get the result as the milliseconds-since-The-Epoch value
You'd notice a difference if you were formatting a date or parsing one, but you aren't doing either of those things.
I need to get start of day and end of day. I have been reading that if I am going to do date stuff to use momentjs. While I might go that route right now I do not think I will need that much date manipulation so am gonna try to not add more npm packages to this project than needed. I was reading that you can set a date to start of day with .setHours(0,0,0,0) though when I try this in my terminal I am seeing the hours get set to T07:00:00.000Z can someone explain why? Feels like it should be T00:00:00.000Z
let date = new Date('2019-08-16T20:30:38Z');
date.setHours(0,0,0,0);
console.log(date);
I live in the Central timezone, UTC -5 this time of year, so I get T05:00:00.000Z when I run it. Since you live in the Pacific timezone (presumably), UTC -7, you get 7am UTC. You are setting the local time but outputting the time in UTC. From the documentation (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setHours):
The setHours() method sets the hours for a specified date according to local time, and returns the number of milliseconds since January 1, 1970 00:00:00 UTC until the time represented by the updated Date instance
As title, although I set with only value.
In Javascript:
var n = (new Date("2015 Oct 17")).getTime()/1000;
console.log(n);
// result: 1445014800
And PHP:
$unix = date('d-m-Y', 1445014800);
echo $unix;
// result: 16-10-2015
Please leave some explanations.
Thanks a lot!
In your JavaScript:
var n = (new Date("2015 Oct 17")).getTime()/1000;
console.log(n);
// result: 1445014800
The operation /1000 is coercing your value to a numeric type, so the answer is correct!
In javascript, parsing of a string like "2015 Oct 17" is entirely implementation dependant. If it works at all, it is likely to be converted to a Date object representing the date at 00:00:00 at the start of the day in the time zone of the host system.
For a system whose time zone offset is, say, UTC+1000, and that parses the string as a local time, the time value in seconds will be 1445004000.
However, such a system might decide that the string is a bit like an ISO 8601 string, and it might decide that since such strings without a time zone were treated as UTC in ES5, that it will treat it as a UTC time. In that case, the time value in seconds will be 1445040000 (i.e. equivalent to 2015-10-17T00:00:00Z).
To reliably transfer dates between systems, it is often considered best to transfer time values in either seconds or milliseconds since the UNIX (and ECMAScript) epoch of 1070-01-01T00:00:00Z.
To create such a time value for 2015-Oct-17 you can use:
var timeValue = Date.UTC(2015, 9, 17);
To convert the UNIX time value 1445014800 to an ECMAScript date, you can do (noting that UNIX time values are in seconds and ECMAScript in milliseconds):
console.log(new Date(1445014800*1000).toISOString()); // 2015-10-16T17:00:00.000Z
So I'll assume that the PHP host is in a timezone that is UTC-05:00 and that 2015-Oct-17 has been treated as UTC.
I have checked it, and this JS function returns
1445036400
which in 'human time' is
Fri, 16 Oct 2015 23:00:00 GMT
https://jsbin.com/wuvawupede/edit?js,console,output
http://www.epochconverter.com/
As w3schools say, Date.parse() returns "the number of milliseconds between the date string and midnight of January 1, 1970."
Which means that
if I write Date.parse("January 1, 1970 00:00:00"), it should give me answer 0.
if I write Date.parse("January 1, 1970 00:00:05"), it should give me answer 5000.. But Im getting the -14395000... Why is that?
You don't specify a time zone so January 1, 1970 00:00:00 is with the time offset of your timezone (or more precisely the one the browser has chosen for you). The milliseconds that are returned are relative to to the UTC.
MDN Date.parse:
The Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.
[...] If you do not specify a time zone, the local time zone is assumed. GMT and UTC are considered equivalent. The local time zone is used to interpret arguments in RFC2822 Section 3.3 format that do not contain time zone information. [...]
I'm having the hardest time trying to convert this date from an API to UTC milliseconds. As of right now I'm displaying the dates but it's showing 7 hours ahead and going on to the next day which I don't even have data for. Here is the example format:
8/31/2012 9:00:00 AM
I currently have this code
var formattedDate = new Date(data[i].Time);
formattedDate = formattedDate.getTime();
which seems like it's returning the correct value type but the date is wrong. I've also tried
getUTCMilliseconds() and returns 0.
EDIT: jsfiddle example : http://jsfiddle.net/b2NK6/
So you want the raw timestamp in UTC time, instead of local time?
Compare:
(new Date(Date.UTC(2012, 7, 31, 9, 0, 0, 0))).getTime(); /* month 7 is August */
with
(new Date(Date.parse("8/31/2012 9:00:00 AM"))).getTime();
When you parse the string (the second example) it applies your local timezone information when it creates the date object. If you are in timezone -0700, then the date that is created will actually correspond to 4:00pm UTC.
But if you create the date object by explicitly saying that you are specifying the UTC value, it will give you 9:00am UTC, which corresponds to 2:00am in timezone -0700.
Edited to give clearer and more correct code example.
var dateString = "8/31/2012 9:00:00 AM"; // assuming this is expressed in local time
var millisecondsSinceTheEpoch = (new Date(dateString)).valueOf(); // 1346418000000
var isoString = (new Date(millisecondsSinceTheEpoch)).toISOString(); // 2012-08-31T13:00:00.000Z
// Note: example return values from a computer on U.S. Eastern Daylight Time (-4:00).
From W3Schools:
The valueOf() method returns the primitive value of a Date object.
Note: The primitive value is returned as the number of millisecond[s] since midnight January 1, 1970 UTC.
Also see W3Schools for a comprehensive overview of the Date object.
HighStocks expects to get its dates aligned to UTC-midnight date boundary.
Assuming your chart only deals with dates (without the time component) here is a trick you can use:
Do originalDate.getTime() to get the number of milliseconds since midnight UTC 1/1/1970 , e.g. 1362286800000.
Divide the number of milliseconds by (1000*60*60*24) to get the number of days since midnight UTC 1/1/1970 e.g. 15767.208333333334.
Do Math.round() to round the number to the nearest UTC midnight, e.g. 15767.
Multiply the number by (1000*60*60*24) to get it back into the milliseconds scale e.g. 1362268800000.
Here is the final formula:
var utcMidnight=new Date(Math.round(anyZoneMidnight.getTime()/86400000)*86400000)