How to format default date in javascript? - 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

Related

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' });

What is the best way of converting Fri Nov 28 2014 16:00:00 GMT 0530 (India Standard Time) to 2014-11-28 16:00:00 in javascript

Fri Nov 28 2014 16:00:00 GMT 0530 (India Standard Time) to 2014-11-28 16:00:00
I want the conversion to be in javascript only. In php some minutes goes up and down when i m using
date('Y-m-d H:i:s', strtotime(Wed Nov 26 2014 04:30:00 GMT 0530 (India Standard Time)));
gives me : 1970-01-01 05:30:00.
date('Y-m-d H:i:s',time(Wed Nov 26 2014 03:30:00 GMT 0530 (India Standard Time))); gives me 2014-11-26 17:32:04
2mins and 4secs are extra.
Thanks.
var d = new Date("Fri Nov 28 2014 16:00:00 GMT 0530");
console.log(getFormattedString(d));
function getFormattedString(d){
return d.getFullYear() + "-"+(d.getMonth()+1) +"-"+d.getDate() + ' '+d.toString().split(' ')[4];
// for time part you may wish to refer http://stackoverflow.com/questions/6312993/javascript-seconds-to-time-string-with-format-hhmmss
}
Use moment.js
moment().format("YYYY-MM-DD HH:mm:ss")
Something like that.
But you should dig a little bit more around here.. There are many similar questions
http://momentjs.com/docs/#/displaying/format/

Date constructors provide unexpected results when called with similar arguments

I got one weird issue with Date object initialization. And wondering if someone can explain why..
var exp1 = new Date('2014-10-17');
var exp2 = new Date(2014,9,17);
var exp3 = new Date('17 Oct 2014');
console.log(exp1);
console.log(exp2);
console.log(exp3);
Results:
Thu Oct 16 2014 18:00:00 GMT-0600 (MDT) // 16th?
Fri Oct 17 2014 00:00:00 GMT-0700 (MST) // Why GMT -7
Fri Oct 17 2014 00:00:00 GMT-0600 (MDT) // The only one that works as expected
Why are these three Date objects so different?
The first date is treated as GMT since no time zone offset is provided. When logged out it shows the time in your local timezone. Adding an offset (exp4 below), I get the date expected.
var exp1 = new Date('2014-10-17');
var exp2 = new Date(2014,9,17);
var exp3 = new Date('17 Oct 2014');
var exp4 = new Date('2014-10-17z-0500');
Results:
Thu Oct 16 2014 19:00:00 GMT-0500 (Central Daylight Time)
Fri Oct 17 2014 00:00:00 GMT-0500 (Central Daylight Time)
Fri Oct 17 2014 00:00:00 GMT-0500 (Central Daylight Time)
Fri Oct 17 2014 00:00:00 GMT-0500 (Central Daylight Time)
I am not sure about exp2 for you, but suspect it has something to do with daylight savings time and that you live in an area that does not observe daylight savings (Arizona?).
Edit: this seems to be browser specific. The results above were generated in Chrome while in IE 11, exp4 was an invalid date. For IE 11 I had to use this format:
var exp4 = new Date('2014-10-17T00:00-05:00');

Is it a bug that javascript doesn't handle days that were skipped for historic reasons?

Some on here may know this, some may not. The date of Sept 3, 1752 doesn't exist. You can check this out for yourself at a number of places such as this one.
I was interested in how javascript and the Date() function would handle this, so I tried the following code:
var sept2 = new Date(1752, 8, 2);
var next = new Date(sept2);
next.setDate(sept2.getDate() + 1);
console.log(sept2, next);
I was suprised to see the results:
Date {Sat Sep 02 1752 00:00:00 GMT-0400 (Eastern Standard Time)}
Date {Sun Sep 03 1752 00:00:00 GMT-0400 (Eastern Standard Time)}
There are two errors here:
the 3rd didn't exist.
The 2nd should be a Wednesday. It's NOT on a Saturday.
So...are these two issues bugs in Date()?
In the British speaking parts of the world, there were no dates between September 2 and the 14th. You went to bed on the second and woke up on the 14th. For much of the rest of the European world, the change happened on October 4, 1582. The next date was October 15, the first day of the Gregorian calendar. Calculating Dates before 1970, be sure you know what calendar you are using.
There's no handling of the transition from Julian to Gregorian calender, so your observation is right.
The 2nd bug is caused because date in javascript are calculated from January 1st, 1970.
You can easily test it by looking at september 14, 1952 day (Tuesday) which is right.
Thu Sep 14 1752 00:00:00 GMT-0400 (Est) <- ok
Wed Sep 13 1752 00:00:00 GMT-0400 (Est) <- did not happen
Tue Sep 12 1752 00:00:00 GMT-0400 (Est)
Mon Sep 11 1752 00:00:00 GMT-0400 (Est)
Sun Sep 10 1752 00:00:00 GMT-0400 (Est)
Sat Sep 09 1752 00:00:00 GMT-0400 (Est)
Fri Sep 08 1752 00:00:00 GMT-0400 (Est)
Thu Sep 07 1752 00:00:00 GMT-0400 (Est)
Wed Sep 06 1752 00:00:00 GMT-0400 (Est)
Tue Sep 05 1752 00:00:00 GMT-0400 (Est)
Mon Sep 04 1752 00:00:00 GMT-0400 (Est)
Sun Sep 03 1752 00:00:00 GMT-0400 (Est)
Sat Sep 02 1752 00:00:00 GMT-0400 (Est) <- did happen, but wrong day of week.
Fri Sep 01 1752 00:00:00 GMT-0400 (Est) <- same
I suppose that every date before september 14, 1752 will have a wrong day of the week.

Parse String Dates with different timezones

I am trying to be able to take many strings with many different formats and in different timezones and turn them into either UTC or my localtime. I have tried the following and for some reason it has given me a hour off:
var moment = require('moment');
console.log(moment('Mon, 30 Sep 2013 18:00:00 EST').format()); //2013-09-30T16:00:00-07:00
console.log(new Date('Mon, 30 Sep 2013 18:00:00 EST')); //Mon Sep 30 2013 16:00:00 GMT-0700 (PDT)
console.log(new Date()); //Mon Sep 30 2013 15:00:00 GMT-0700 (PDT)
The only thing I can think of that could cause this is day light savings time but I am not sure. Any suggestions with how to proceed?
You used the wrong time zone. For an apples-to-apples comparison, use EDT (eastern daylight time):
> console.log(new Date('Mon, 30 Sep 2013 18:00:00 EDT'));
Mon Sep 30 2013 15:00:00 GMT-0700 (PDT)
which is what you would expect (3 hour difference)

Categories

Resources