Why new Date(yyyy-mm-dd) shows past date? [duplicate] - javascript

This question already has answers here:
Javascript: parse a string to Date as LOCAL time zone
(3 answers)
Closed 3 years ago.
why its showing past day date
var date = new Date('2020-01-01');
console.log(date)
Tue Dec 31 2019 19:00:00 GMT-0500 (Eastern Standard Time)

Because of your timezone settings (Easter Standard time is GMT -0500 therefore 5 hours before 2020-01-01).
Javascript Date object are timestamps - they merely contain a number
of milliseconds since the epoch. There is no timezone info in a Date
object. Which calendar date (day, minutes, seconds) this timestamp
represents is a matter of the interpretation (one of to...String
methods).
Basically it is the toString method that converts the date to your local timezone.
Source

Related

Unix timestamp is not correct in JavaScript, but is correct according to other sources [duplicate]

This question already has answers here:
Convert UNIX timestamp to date time (javascript)
(4 answers)
Closed 2 years ago.
This timestamp 1604978063 is not correct according to new Date(1604978063)
It returns the following:
Date Mon Jan 19 1970 05:49:38 GMT-0800 (Pacific Standard Time)
When I look it up on DuckDuckGo, it looks correct.
What's happening here?
Javascript's timestamps are not standard unix timestamps. They use milliseconds and therefore require a multiplication by 1000:
new Date(1604978063 * 1000)

Transform format of date [duplicate]

This question already has answers here:
How to get date in UTC format irrespective of current system date with javascript?
(4 answers)
Closed 2 years ago.
I have following date Thu Apr 02 2020 11:57:21 GMT+0200 (Středoevropský letní čas). I need to compare this date with the date that is stored in API. The problem is that the date in API is in this format 2020-06-27T12:34:00.000Z.
Is there any way I can transform the format of the first date to be the same as the second date? Or can I comprare them in some way, that the formats wont matter?
Thank you in advance.
Use Date.prototype.toISOString()
The toISOString() method returns a string in simplified extended ISO format (ISO 8601),
which is always 24 or 27 characters long (YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ, respectively).
The timezone is always zero UTC offset, as denoted by the suffix "Z".
const time = 'Thu Apr 02 2020 11:57:21 GMT+0200 (Středoevropský letní čas)'
const result = new Date(time).toISOString();
console.log(result);
You can do this
const date = new Date("Thu Apr 02 2020 11:57:21 GMT+0200 (Středoevropský letní čas)");
const formattedDate = date.toISOString(); // This the formatted date

javascript new Date with string param has wrong date [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 5 years ago.
I am creating a new date from string
var s = "2017-12-06"
var dt = new Date(s)
console.log(dt) // outputs Tue Dec 05 2017 19:00:00 GMT-0500 (EST)
What am I missing ?
Date.toString() is formatted in your local time zone, but because you've passed in an ISO-8601 string, the value is parsed as if it's UTC.
From the Date.parse() documentation (as the Date(String) constructor is documented to behave like Date.parse):
The date time string may be in a simplified ISO 8601 format. For example, "2011-10-10" (just date) or "2011-10-10T14:48:00" (date and time) can be passed and parsed. Where the string is ISO 8601 date only, the UTC time zone is used to interpret arguments. If the string is date and time in ISO 8601 format, it will be treated as local.
So you'll end up with a Date which is equivalent to 2017-12-06T00:00:00Z. But Date.toString() shows you that instant in time in your current time zone - and if you're in America/New_York or a similar time zone which is 5 hours behind UTC at that moment in time, that means it'll print December 5th at 7pm.

Showing correct date from API [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Javascript: parse a string to Date as LOCAL time zone
(3 answers)
Closed 5 years ago.
I'm getting crazy with showing a date correctly to the client:
From the API I receive data.expiry_date and its value is: 2017-09-06T23:59:59Z
The client to show that data looks like this:
var date = new Date(data.expiry_date);
$('#expiry_date').val(`${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}`);
The result is a day before than expected: it should be 06/09/2017 but it show 07/09/2017.
Basically the date value from the action var date = new Date(data.expiry_date); is: Thu Sep 07 2017 01:59:59 GMT+0200 (CEST).
How can I get rid of it?
You're not in the same time zone.
2017-09-06T23:59:59Z is UTC
which is same as
Thu Sep 07 2017 01:59:59 GMT+0200 (CEST)

Why is new Date() removing a day? - Javascript [duplicate]

This question already has answers here:
Why isn't "2016-02-16" equal to "2016-02-16 00:00"?
(5 answers)
Closed 6 years ago.
I am creating a date with new Date(). When I do this, it is subtracting a day. Here is the code:
var dateString = "2016-04-10";
var date = new Date(dateString);
// date = Sat Apr 09 2016 18:00:00 GMT-0600 (MDT)
What do I misunderstand? Why is the date not Apr 10 2016? How do I make this work properly?
Your timezone is GMT-6 (as revealed by the GMT-0600 (MDT) in the output you've provided). Therefore the date which gets generated is offset by -6 hours. In this case, midnight minus 6 hours is 6PM on the previous day.
If you call date.toISOString(), you'll see that the UTC time is "2016-04-10T00:00:00.000Z" as expected.

Categories