JavaScript get date and day using Date.parse - javascript

by using:
Date.parse('2015-01-01');
it gives output: Thu Jan 01 2015 05:00:00 GMT+0500 (Pakistan Standard Time)
but i want just like this: Thu Jan 01 2015
(moreover i am using it in morris.js chart)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Conversion_getter
new Date('2015-01-01').toDateString();

Related

How to format default date in javascript?

I'm trying to format an string date that looks like this:
Tue Mar 13 2018 00:00:00 GMT-0600 (Mountain Daylight Time)}
to:
"2018-03-13T00:00:00",
Can anyone tell me what I'm missing or need to add? Thanks a lot in advance!
let someDate = 'Tue Mar 13 2018 00:00:00 GMT-0600 (Mountain Daylight Time)}';
console.log(someDate.replace(/T.+$/, "T00:00:00"));
let someDate = 'Tue Mar 13 2018 00:00:00 GMT-0600 (Mountain Daylight Time)';
console.log(new Date(someDate).toISOString().replace(/T.+$/, "T00:00:00"));
someDate.toISOString() will give you the date in ISO format

getting incorrect time while converting string into date object

I am using calling this function getFormattedTimeFromString(startTime)
getFormattedTimeFromString(timeString){
return (new Date('1970-01-01T' + timeString + 'Z'));
}
if you are passing in getFormattedTimeFromString("14:00:00") and getting Thu Jan 01 1970 19:30:00 GMT+0530 (IST) as Output well then that is expected...
What do you want it to be? Maybe you want to remove the Z? for it to be local?
the Thu Jan 01 1970 19:30:00 GMT+0530 (IST) is just a representation in your local timezone
while i get Thu Jan 01 1970 15:00:00 GMT+0100 (CET)
but it still being the same in UTC.
If you would do:
new Date(`Thu Jan 01 1970 19:30:00 GMT+0530 (IST)`).toJSON()
// you get same input back
"1970-01-01T14:00:00.000Z"
which is still the same input
You also have diffrent method on the date object like getUTCxxxx if that is what u want

Create javascript Date() object to Amrica/New_York timezone

The code:
console.log(start)
reads, Thu Mar 01 2018 00:00:00 GMT+0530 (India Standard Time)
I want it a new object start_NY which will read Thu Mar 01 2018 00:00:00 w.r.t. America/New_York timezone. Something like Thu Mar 01 2018 00:00:00 GMT-0500.
I used a script:
start_ny = new Date('Thu Mar 01 2018 00:00:00 GMT-0500');
But that reads, Thu Mar 01 2018 10:30:00 GMT+0530 (India Standard Time), which is actually converting the date into Indian Standard Time, instead of giving me a time from New_York timezone.
The format should be same as that of the existing one. How can I do that?
Try with this following code and MDN resources
new Date().toLocaleString('en-US', { timeZone: 'America/New_York' })
// Date Format
new Date().toDateString('en-US', { timeZone: 'America/New_York' });

Date returning inconsistent results (depending whether leading zero exists)

> new Date('2015-1-1')
Thu Jan 01 2015 00:00:00 GMT-0500 (EST)
> new Date('2015-01-1')
Thu Jan 01 2015 00:00:00 GMT-0500 (EST)
> new Date('2015-1-01')
Thu Jan 01 2015 00:00:00 GMT-0500 (EST)
// Yet...
> new Date('2015-01-01')
Wed Dec 31 2014 19:00:00 GMT-0500 (EST)
// Similarly:
> new Date('2015-1-10')
Sat Jan 10 2015 00:00:00 GMT-0500 (EST)
> new Date('2015-01-10')
Fri Jan 09 2015 19:00:00 GMT-0500 (EST)
Can't figure out why this is happening (Chrome 39). Is it related to octal parsing?
Firefox only accepts new Date('2015-01-10'), and returns what I expect: Date 2015-01-10T00:00:00.000Z
Found the answer in a related question; it appears Chrome parses the YYYY-MM-DD format as UTC time, then converts it the local timezone. So, 2015-01-01 00:00:00 in UTC is Dec 31 in EST.
See Inconsistencies when creating new date objects:
It looks like the form '1979-04-05' is interpreted as a UTC date (and then that UTC date is converted to local time when displayed).
Apparently, a possible cross browser solution is to replace the dashes with slashes to force using local time:
new Date('2015-01-10'.replace(/-/g, '/'))
I am unsure of your problem since My chrome(39.0.2171.99) gives me Jan 01 in all case. But having said this, I would like to point out that you should probably use
new Date(2015,1,1)
This is how JS Date is supposed to be initialised.

Javascript Date constructor uses arbitrary time

When I construct a date object from a string, I am getting confusing results. It seems as if the time is chosen arbitrarily (but repeatably) if I don't specify it.
var d1=new Date("2013-10-9"), d2=new Date("2013-10-10");
output = d1+' '+d1.toUTCString()+'<br>\n';
output += d2+' '+d2.toUTCString()+'<br>\n';
Chromium 20.0...
Wed Oct 09 2013 00:00:00 GMT-0600 (MDT) Wed, 09 Oct 2013 06:00:00 GMT
Wed Oct 09 2013 18:00:00 GMT-0600 (MDT) Thu, 10 Oct 2013 00:00:00 GMT
Why would Chromium choose a different time on October 10?
By the way, the workaround is here: https://stackoverflow.com/a/744134/86967
It has to do with the format of the date string you are using. If you specify 2013-10-09 (notice the extra 0 on the day), then it works as expected. If you use 2 digits for the day and month, then you are following the ECMA spec.
var d1=new Date("2013-10-09"), d2=new Date("2013-10-10");
console.log(d1+' '+d1.toUTCString());
console.log(d2+' '+d2.toUTCString());
Yields:
Tue Oct 08 2013 20:00:00 GMT-0400 (Eastern Daylight Time) Wed, 09 Oct 2013 00:00:00 GMT
Wed Oct 09 2013 20:00:00 GMT-0400 (Eastern Daylight Time) Thu, 10 Oct 2013 00:00:00 GMT
I believe the code they are using can be found here:
https://github.com/WebKit/webkit/blob/master/Source/WTF/wtf/DateMath.cpp
When you provide an ECMA date, it will use the parseES5DateFromNullTerminatedCharacters method to parse the date, but when you use a non-standard date format it will use the parseDateFromNullTerminatedCharacters method. I am not that familiar with the webkit code, so I could be wrong, but this is based on my reading of the parsing logic.
The standard date format can be found in section 15.9.1.15 of the ECMA Spec.

Categories

Resources