Date wrong in JS [duplicate] - javascript

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 1 year ago.
Why when I create without space in javascript console is to the yesterday date at 9:00PM, but when I write with a space in the end the date and hour is ok?
new Date('2021-07-20')
Wrong: Mon Jul 19 2021 21:00:00
new Date('2021-07-20 ')
Correct: Tue Jul 20 2021 00:00:00

2021-07-20 is a valid ISO time. It gets parsed as 20th July 2021 at midnight UCT. It then gets converted to local time for you which (in Porto Alegre) is three hours earlier.
2021-07-20 is not a valid ISO time, so it hits your JS engine's implementation-specific parser for non-standard date formats.

You shoud use , between them like:
const d = new Date(2018, 11, 24, 10, 33, 30);
And remember JavaScript counts months and days from 0 to 11 just like indexes.

Related

JavaScript .toISOString() function returning incorrect date [duplicate]

This question already has answers here:
toISOString() return wrong date
(3 answers)
Format JavaScript date as yyyy-mm-dd
(50 answers)
Closed 4 months ago.
Here is my Cypress JS code:
cy.get('creationDateElement').then(date => {
const reformattedDate = new Date(date.text())
cy.log('Reformatted Date: ' + reformattedDate)
cy.log('ISO string: ' + reformattedDate.toISOString().split('T')[0])
})
I am trying to transform the date so that it returns 2015-09-11
Here is what is logged with the above code:
Reformatted Date: Fri Sep 11 2015 00:00:00 GMT+0100 (British Summer Time)
ISO string: 2015-09-10
As you can see, the day being returned is 10 rather than 11, but I don't know why this is happening.
Can someone please tell me why this is happening, & how I can return 2015-09-11 instead?
The code is working with the below dates though, so I don't know why the one above is causing an error:
Reformatted Date: Wed Feb 28 2007 00:00:00 GMT+0000 (Greenwich Mean Time)
ISO string: 2007-02-28
This is part of a Cypress UI test. So the actual date comes from an element on the UI that contains a date:

JavaScript Date() parsing time string incorrectly [duplicate]

This question already has answers here:
What are valid Date Time Strings in JavaScript?
(2 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 1 year ago.
I have written a helper function to attempt to parse a time string in a flexible way. Sometimes the time comes in as a full date but sometimes its just a string like "10:30" or "12:01 PM".
So I am checking to see if I can parse the date first:
let date = new Date(value);
if (!isNaN(date.getTime()) // its a valid date
If that fails then I fall back to regex to check if I can make any sense of the string manually:
const isValidTime = /^([0-1]?[0-9]|2[0-4]):([0-5][0-9])(:[0-5][0-9])?(\s?(AM|PM))?$/i.test(value);
While writing unit tests I found some weird results. Incorrect times were returning actual dates in stead of throwing an error. Javascript new Date() is parsing some of the strings and seemingly making up dates for them but also strangely, its getting the hour right too. Here are some examples of time strings that are causing this:
new Date("1:60") // Fri Jan 01 1960 01:00:00 GMT+0200
new Date("2:70") // Thu Jan 01 1970 02:00:00 GMT+0200
new Date("3:80 PM") // Tue Jan 01 1980 15:00:00 GMT+0200
//and a "valid" time for good measure
new Date("5:30") // Invalid Date
What the heck is going on here?

new Date('dd/mm/yyyy') instead of newDate('mm/dd/yyyy') [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
How to convert dd/mm/yyyy string into JavaScript Date object? [duplicate]
(10 answers)
Closed 4 years ago.
Is it possible to enter date in dd/mm/yyyy format into newDate (e.g. 03/01/2018) so that it returns object Wed Jan 03 2018 00:00:00 GMT+0000 (Greenwich Mean Time) {}?
If I have a date 03/01/2018 it returns Thu Mar 01 2018 00:00:00 GMT+0000 (Greenwich Mean Time) {} instead... I'm not trying to pass mm/dd/yyyy format..
I've looked at plenty of posts on this and can't find an answer to my question.
You have no control over the Date constructor, so you need to feed it a date in the format that it wants. Since you are formatting the date yourself, it is better to use the other Date constructor, which takes the year, monthIndex, and day as arguments, since it is more bullet-proof across different browsers and runtimes:
function my_date(date_string) {
var date_components = date_string.split("/");
var day = date_components[0];
var month = date_components[1];
var year = date_components[2];
return new Date(year, month - 1, day);
}
console.log(my_date("03/01/2018"));
The case of dates one area where I install the moment library on nearly every JavaScript project I create.
Note: Snippet may display the result differently; check your console.
You could use regex like so:
var date = new Date("03-01-2018".replace( /(\d{2})-(\d{2})-(\d{4})/, "$2/$1/$3"))
I suggest you use momentjs from http://momentjs.com/ . it is very easy to use to output any format u want.
moment().format('MMMM Do YYYY, h:mm:ss a'); // June 28th 2018, 10:30:09 pm
moment().format('dddd'); // Thursday
moment().format("MMM Do YY"); // Jun 28th 18
moment().format('YYYY [escaped] YYYY'); // 2018 escaped 2018
moment().format();

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.

javascript new Date() issue [duplicate]

This question already has answers here:
javascript is creating date wrong month
(4 answers)
Closed 8 years ago.
I'm facing a date issue with new Date() function. May i know why is this weired output is coming.
new Date(1984, 12, 13)
Sun Jan 13 1985 00:00:00 GMT-0500 (EST)
Here the month should be December but it results me with Jan.
January is 0 in javascript , so javascript is counting up and is adding the date. That's why you get this result.

Categories