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

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

Related

Is there a way to modify date iso string such that in JS, new Date(isoString) does not return date in daylight saving time

After setting system clock to EST, on converting iso string to date object using Date constructor we noticed that dates between March and November return offset time -4 hrs i.e. daylight saving time and otherwise offset is -5hrs.
For eg, in below screenshot ISO date string is of September month (i.e between March & November) & date returned from new Date() is having offset of -4 (underlined)
And for January month , returned date is having offset of -5 (underlined)
So on calculating the difference in days between the above 2 dates gives the difference in decimal, where the expectation for the difference is that it should be an integer value.
I am calculating the difference using (a.getTime() - b.getTime())/(24*3600000).
Is there a way to modify date iso string such that in javascript, new Date(isoString) does not return date in daylight saving time ?
The automatic change of offset from -5 to -4 & vice versa is causing problems in calculating difference in days in integer and also on adding number of days to a date.
Or is there a better way to handle these issues by an alternate approach? If this can be handled using moment, I'm fine with it.
Please post text, not images.
The strings in the OP are in a format supported by the built–in parser, so should be parsed correctly by modern browsers. As they don't have an offset, they're parsed as local. That results in a time value that is an offset from 1970-01-01T00:00:00Z and is used for the new Date instance.
If the host system is set for a place or region that observes daylight saving at the dates and times represented by the timestamp, it will be used to calculate the time value. If one is during a period of DST and one isn't, then the time difference between them will not be an integer multiple of 24 hours (or 8.64e7 milliseconds).
If you want every day to be 24 hours long, use UTC for everything (including the strings you're parsing). One way is using date–only ISO 8601 strings, such as 2024-09-29. e.g.
let days = (new Date('2024-09-29') - new Date('2023-01-02')) / 8.64e7;
console.log(days); // 636
// or
console.log((Date.UTC(2024,8,29) - Date.UTC(2023,0,2)) / 8.64e7)

convert a string to date object javascript returns an incorrect date

I am trying to convert string to a date object in javascript, however what i day that is minus 1 from day in string. I don't know what is wrong. Here is the method
function formatDate(date_str)
{
console.log(date_str); //input : 2020-03-11
let new_date = new Date(date_str);
console.log(new_date); //output : Tue Mar 10 2020 20:00:00 GMT-0400 (Eastern Daylight Time)
return new_date;
}
The most likely explanation is that parsing the input string "2020-03-11" with no other information equates it to a date of March 11, 2020 at midnight UTC. When you are in a different time zone, then it calculates your time zone offset and gives you a time four hours earlier which would be the day before in local time.
Why such behavior:
The date string(2020-03-11) did not specify any time zone, when you attempt to create a Date object with this string, JavaScript would assume the time zone to be UTC so the date is internally dealt with like as: 2020-03-11T00:00:00Z.
console.log(new_date) would internally call .toString() method on the new_date object and doing that would trigger a date conversion to your local time zone. From the question I believe you(the time on your machine actually) are in GMT-4, this is why 4 hrs is being subtracted from the output of the logs. More details about the date conversion due to time zone here
Possible Fix:
Firstly, we should understand that this is not a bug or an error, it is just how the JavaScript Date object works.
For the scenario described in your question, I'm guessing what you want is to avoid this time zone conversion on the date string. What you can do is add timezone information to the date string before using it to instantiate a date object, with this, javascript wouldn't assume that the date string you are passing into the Date() constructor is in UTC, and when you call Date.toString() or any other similar methods, there won't be any surprises. An implementation for this can be something like this:
// Sorry for the super long function name :)
function add_local_time_zone_to_date_string (date_string) {
// Getting your local time zone
let local_time_zone = (new Date().getTimezoneOffset() * -1) / 60;
// local_time_zone is a number, convert it to a string
local_time_zone = (local_time_zone.toString())
// Padding with 0s if needed to ensure it is of length 2
local_time_zone = local_time_zone.padStart(2, '0');
return `${date_string}T00:00:00+${local_time_zone}`
}
function formatDate(date_str) {
console.log(date_str); //input : 2020-03-11
const date_with_time_zone = add_local_time_zone_to_date_string(date_str);
let new_date = new Date(date_with_time_zone);
console.log(new_date); //output : There should be no surprises here
return new_date;
}

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

Javascript dates are a day off? [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Changing the format of a date string
(2 answers)
Closed 5 years ago.
I am trying to use a simple date function in my application to pass a date in the format of yyyy-mm-dd such as 2017-07-30 and have it returned in the format of 07/30/2017.
However, when I try this, I supply my date correctly but it outputs one day shorter than what I am looking for.
function format(inputDate) {
var date = new Date(inputDate);
if (!isNaN(date.getTime())) {
var day = date.getDate().toString();
var month = (date.getMonth() + 1).toString();
// Months use 0 index.
return (month[1] ? month : '0' + month[0]) + '/' +
(day[1] ? day : '0' + day[0]) + '/' +
date.getFullYear();
}
}
console.log(format('2017-07-30'));
Here is a fiddle: http://jsfiddle.net/49pptrj4/
Any thoughts as to why this is returning incorrectly?
Result on my end:
From here
Given a date string of "March 7, 2014", [Date.]parse() assumes a local time zone, but given an ISO format such as "2014-03-07" it will assume a time zone of UTC.
Your date string is assumed to be 0:00, or midnight, on the date specified in UTC, the time zone of Greenwich, England. Your browser however takes this time and converts it to your local timezone, which is a few hours behind UTC if you're in the Americas, making the result a day behind.
The following code should work for creating a Date in the local timezone with the correct date.
utcDate = new Date("2017-07-30"); //Date object a day behind
new Date(utcDate.getTime() + utcDate.getTimezoneOffset() * 60000) //local Date
Here the local Date is created by adding time based on the time zone difference. getTimezoneOffset() returns in minutes, so * 60000 is needed to convert to milliseconds.
This might not work in areas ahead of UTC; it might advance an extra day.
Edit: Just checked and getTimezoneOffset() is negative in areas ahead of UTC so it will subtract time correctly.

Trouble converting time to UTC for highstocks

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)

Categories