Javascript date object automatically add one day when creating from date string - javascript

In my javascript i want to convert date from date string.
i have string like
date = "Thu Sep 03 2015 19:30:00 GMT+0000"
Now i convert string using Date object.
var d = new Date(date);
But this gives me,
Fri Sep 04 2015 01:00:00 GMT+0530 (IST)
It automatically add one day into day. What is wrong?

It automatically add one day into day. What is wrong?
Nothing. The time you input is 19:30 GMT and the timezone on the device you're using is set to GMT+0530. Add 5 hours 30 minutes to 7:30pm and you get 01:00am the following day.
You should not use the Date constructor to parse strings, as it is inconsistent across browsers and until recently, entirely implementation dependent. Manually parse strings, or use a Date library.

Related

Formatting and parsing a date string without timezone conversion

If I'm in a timezone a few hours before midnight and I try
new Date("2020-03-30T00:00:00.000Z")
it will result in
Sun Mar 29 2020 18:00:00 GMT-0600 (Central Standard Time)
then I realized even
new Date("2020-03-30")
in the wrong timezone will result in the exact same conversion. I have a similar problem with moment.js constructed from similar strings in similar timezones, when I try to call the .format() method it will do a conversion first, sometimes resulting in the wrong day of the month.
How can I format such a date initialized like this and not have to deal with timezone conversions? eg work with just the "raw" date it was initialized from

javascript date format conversion (full text date to unix time stamp)

I have a date in the following format
Fri Mar 16 2012 05:53:18 GMT 0200 (GTB Standard Time)
And I want to convert it into a unix timestamp.
Until now I manually split the string by spaces and then I am giving it as an input to a Date object, in order to get milliseconds in a latter step.
Is there any easiest way?
(I am trying to avoid jQuery plug-ins and do it using vanila javascript)
Yes, the easiest way would be to pass the string to Date object and then call the getTime method:
var myDate = new Date('Fri Mar 16 2012 05:53:18 GMT+0200 (GTB Standard Time)');
console.log( myDate.getTime() ); //1331869998000
No need to split your string by spaces.

Why does js subtract a day from a Date object with a certain format?

I get dates from the database in this format:
yyyy-mm-dd
When I create a javascript Date object using this string, it builds a day before the date.
You can test this in your console:
var d = new Date("2015-02-01");
d
You will get January 31st! I've tested many theories, but none answer the question.
The day is not zero-based, otherwise it would give Feb 00, not Jan 31
It's not performing a math equation, subtracting the day from the month and/or year
Date(2015-02-01) = Wed Dec 31 1969
Date("2015-01") = Wed Dec 31 2014
It is not confusing the day for the month
Date("2015-08-02") = Sat Aug 01 2015
If this were true the date would be Feb 08 2015
If you create a Date using a different format, it works fine
Date("02/01/2015") = Feb 1st, 2015
My conclusion is that js does this purposefully. I have tried researching 'why' but can't find an explanation. Why does js build dates this way, but only with this format? Is there a way around it, or do I have to build the Date, then set it to the next day?
PS: "How to change the format of the date from the db" is not what I'm asking, and that is why I'm not putting any db info here.
Some browsers parse a partial date string as UTC and some as a local time,
so when you read it the localized time may differ from one browser to another
by the time zone offset.
You can force the Date to be UTC and add the local offset if you
want the time to be guaranteed local:
1. set UTC time:
var D= new Date("2015-02-01"+'T00:00:00Z');
2. adjust for local:
D.setMinutes(D.getMinutes()+D.getTimezoneOffset());
value of D: (local Date)
Sun Feb 01 2015 00:00:00 GMT-0500 (Eastern Standard Time)
Offset will be whatever is local time.
Some differences between browsers when time zone is not specified in a parsed string:
(tested on Eastern Standard Time location)
(new Date("2015-02-01T00:00:00")).toUTCString();
Firefox 35: Sun, 01 Feb 2015 05:00:00 GMT
Chrome 40: Sun, 01 Feb 2015 00:00:00 GMT
Opera 27: Sun, 01 Feb 2015 00:00:00 GMT
IE 11: Sun, 01 Feb 2015 05:00:00 GMT
IE and Firefox set the Date as if it was local, Chrome and Opera as if it was UTC.
In javascript, Date objects are internally represented as the number of milliseconds since Jan 1st 1970 00:00:00 UTC. So instead of thinking of it as a "date" in the normal sense, try thinking of a Date object as a "point in time" represented by an integer number (without timezone).
When constructing your Date object using a string, you are actually just calling the parse function. Most date time formats (including ISO 8601) allow you to reduce the precision of a date string.
For reduced precision, any number of values may be dropped from any
of the date and time representations, but in the order from the least
to the most significant.
e.g. 2015-02-01 would represent the day February 1st 2015.
This causes a dilemma for javascript because a Date object is always accurate to the millisecond. Javascript cannot store a reduced accuracy date since it is just an integer of milliseconds since 1st Jan 1970. So it does the next best thing which is to assume a time of midnight (00:00:00) if not specified, and a timezone of UTC if not specified.
All valid javascript implementations should give the same result for this:
var d = new Date("2015-02-01");
alert(d.getTime());
1422748800000
The out-by-1-day issue comes when outputting the date either to some (often unclear) debugger or using the getter methods because the local timezone is used. In a browser, that will be your operating systems timezone. Anyone "west" of Greenwich Mean Time may see this problem because they have a negative UTC offset. Please note there are UTC equivalent functions too which use the UTC timezone, if you are really just interested in representing a date rather than a point in time.

javascript date issue

Why is it that in javascript I create a new date object mydate = new Date('2011-10-03'); and it prints as October 2nd? Sun Oct 02 2011 18:00:00 GMT-0600 (MDT)
If I set the date to be October 3rd shoudn't I get a 3 when I call mydate.getDate();?
What I am I missing?
I believe your date is off by one because it's being parsed in UTC time and you're displaying it in mountain time (I assume your local time). This is per ECMA spec.
See section 15.9.3.3 of the Javascript specification here:
http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
Try this instead
mydate = new Date('2011/10/03');
I think it is setting the date to 2011-10-03 and the time to 00:00:01 for UTC.
And the print is converting that date object to your local time

Convert date/time in GMT to EST in Javascript

In Javascript, how can I convert date/time in GMT to EST irrespective of user settings?
var tmpDate = New Date("enter any valid Date format here")
The javascript Date() function will automatically convert it to your local time.
Example:
var tmpDate = new Date("Fri Jul 21 02:00:00 GMT 2012");
alert(tmpDate);
//Result: Fri Jul 20 22:00:00 EDT 2012
Try some different values at jsfiddle: http://jsfiddle.net/R3huD/
i was surprise to find the simplest solution.
If you have date in GMT, and when you create date in browser it always create in that time zone.
Simplest way is create date object with GMT itself and then do below
starTime.setHours(starTime.getHours()+(starTime.getTimezoneOffset()/60));
That's it. Even if you have date of future after day light saving like after November then also it will also work.
See here:
https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-6016329.html
all you have to do is get the time in miliseconds and then add the offset in milliseconds and then shift back to a date time object

Categories

Resources