Javascript's Getdate Returns Wrong Date

JavaScript's getDate returns wrong date

The Date.parse method is implementation dependent (new Date(string) is equivalent to Date.parse(string)).

While this format will be available on modern browsers, you cannot be 100% sure that the browser will interpret exactly your desired format.

I would recommend you to manipulate your string, and use the Date constructor with the year, month and day arguments:

// parse a date in yyyy-mm-dd format
function parseDate(input) {
var parts = input.match(/(\d+)/g);
// new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]])
return new Date(parts[0], parts[1]-1, parts[2]); // months are 0-based
}

Getting wrong date while using new Date() in javascript

The confusion here is that getDate uses your local timezone, but toISOString shows you the information in UTC.

If you want the same day value that's in the ISO string (which is in UTC), use getUTCDate:

console.log(new Date("2021-05-05T18:47:00.000Z").getUTCDate());

Javascript getDate Function returns wrong day of the month

Try these and I think you will get some information that will help you:

getFormattedDate(new Date('31-MAR-1944 GMT'));
getFormattedDate(new Date('31-MAR-1944'));

The first one works "incorrectly" because it is parses that particular date format dd-mmm-yyyy string into the GMT time zone and you are apparently some x hours behind GMT, GMT-x. However, the second one works "correctly" because the time is in your own time zone before the function's calculations begin. In fact, if you just examine the basic date value that is getting passed to the function, you may learn all you need. On my computer here's an example:

new Date('31-MAR-1944 GMT')
// Thu Mar 30 1944 17:00:00 GMT-0700 (Pacific Daylight Time)

To translate the date out of your current time zone and back into the original time zone (this gets tricky to say depending on how you look at it), you'll need to do something such as use a different format, remove the time zone from the date string, or account for time zone differences between computers in your organization. You could see if using the getUtcDate and getUtcMonth functions do the job for you, or you could do explicit time zone calculation.

Start with your date object, then getTimezoneOffset() on it. This returns a number in minutes of reverse sign to the GMT offset in hours. Convert the timezone offset of the server time into minutes and appropriately add or subtract. THEN do your date math to get a formatted date.

getDate() giving wrong date due to Daylight saving timezone

What you'd like to do here is adjust the time according to getTimezoneOffset() and then store it.

MDN

The time-zone offset is the difference, in minutes, from local time to
UTC.

var d = new Date();console.log(d)//Time before adjusting the timezoned.setMinutes(d.getTimezoneOffset());console.log(d) //time adjusted to UTC

getDate returning wrong value

You're doing the basic arithmetic. Subtracting 2 from 1 will result in -1. You may try to set the date first and then get the day from the date object.

The reason is that setDate() sets the day relatively when the argument is not in the range (less than or equal to 0).

let start = new Date();let end = new Date();start.setDate(start.getDate() - 2);end.setDate(end.getDate() + 2);console.log(start.getDate(), end.getDate());

Why Date() returns wrong date?

You're adding 7 days to currentDate at line:

currentDate.setDate(currentDate.getDate() + (days * operation));

and subtracting 7 days of currentDate at the line:

prevDate.setDate(currentDate.getDate() - (days * operation));

So your prevDate will have the same value as currentDate initial value.

Correction here:

let operation = 1;
let date: Date = new Date();
let days: number = 7

const currentDate = new Date(date);
const prevDate = new Date(date);

prevDate.setDate(currentDate.getDate() - (days * operation));

console.log(prevDate);

JavaScript's getDate returns wrong date

The Date.parse method is implementation dependent (new Date(string) is equivalent to Date.parse(string)).

While this format will be available on modern browsers, you cannot be 100% sure that the browser will interpret exactly your desired format.

I would recommend you to manipulate your string, and use the Date constructor with the year, month and day arguments:

// parse a date in yyyy-mm-dd format
function parseDate(input) {
var parts = input.match(/(\d+)/g);
// new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]])
return new Date(parts[0], parts[1]-1, parts[2]); // months are 0-based
}


Related Topics



Leave a reply



Submit