Is the JavaScript Date Object Always One Day Off

Is the Javascript date object always one day off?

Notice that Eastern Daylight Time is -4 hours and that the hours on the date you're getting back are 20.

20h + 4h = 24h

which is midnight of 2011-09-24. The date was parsed in UTC (GMT) because you provided a date-only string without any time zone indicator. If you had given a date/time string w/o an indicator instead (new Date("2011-09-24T00:00:00")), it would have been parsed in your local timezone. (Historically there have been inconsistencies there, not least because the spec changed more than once, but modern browsers should be okay; or you can always include a timezone indicator.)

You're getting the right date, you just never specified the correct time zone.

If you need to access the date values, you can use getUTCDate() or any of the other getUTC*() functions:

var d,
days;
d = new Date('2011-09-24');
days = ['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'];
console.log(days[d.getUTCDay()]);

Why a given date turns into a day before when using new Date()?

var dateArray = "2016-02-04".split("-");

var year = dateArray[0];

var month = parseInt(dateArray[1], 10) - 1;

var date = dateArray[2];

var _entryDate = new Date(year, month, date);

alert(_entryDate);

Javascript date objects always one day behind

Assuming your backend date is in UTC specifically ISO8601 and you want the displayed date to be UTC in en-us language-region.

var date = "2016-10-01";

var parts = date.split('-');

parts[1] -= 1;

var d = new Date(Date.UTC.apply(null, parts));

options = {

month: "long",

weekday: "long",

year: "numeric",

day: "numeric",

timeZone: 'UTC'

};

document.getElementById('out').textContent = d.toLocaleDateString("en-us", options);
<pre id="out"></pre>

Why does JavaScript show me one day before my set date?

When you create date from string without timezone you get a date + timezone correction – if you're in USA then you have something like GMT-7 and you get the second of June minus 7 hours – the previous day. Try splitting your date and use new Date(2015, 7, 1) constructor and you'll get date you're expecting. String parse reference docs -https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

java Date object is always one day less

java.util.Date is the most confusing class in Java Core.
I would not recommend to pass dates using java.util.Date, as it is intended to store both date and time.

As I pointed out in comments Apr 21 8pm EDT == Apr 22 midnight GMT. Print your date in GMT timezone and you will get Apr 22 00:00:00:

SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy hh:mm:ss a z");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("Rest service got called:"+ sdf.format(demoBean.getDate()));

But I would rather recommend to send date as string or use java.time.LocalDate.

Luxon substracting one day if JS date is UTC

I try to convert it to a JS date by doing const jsDate = new Date(date)

Uh, why not just date.toJSDate()?

Instead of giving me 2000-06-23, DateTime.fromJSDate(jsDate).toFormat('yyyy-MM-dd') gives me 2000-06-22. Is this a bug on Luxon or I need to do something to get the correct date?

According to its docs, fromJSDate defaults to creating a DateTime instance in the local (system) timezone. If you expect another UTC date back, you need to specify that:

DateTime.fromJSDate(jsDate, {zone: 'utc'}).toFormat('yyyy-MM-dd')

Weird behaviour from JS Date() object

The output

2020-08-31T23:00:00.000Z

is ISO format, and the Z shows the date is in UTC. When you call the Date constructor, it is using your browser timezone by default, which I can assume is +01:00. Moreover, month indice in the constructor is zero-based.

If what you want is the beginning of the month in UTC, you can do:

d = new Date(Date.UTC(2020, 8, 01))
d.toISOString()
"2020-09-01T00:00:00.000Z"


Related Topics



Leave a reply



Submit