JavaScript New Date formats - javascript

I'm trying to achieve this date format..
I'm getting my date from back-end, but its in server time. It looks like this
var d.date = "2017-04-26 07:49:09"
Then, I'm formatting it so date constructor would accept the format properly
var dateFormated = d.date.replace(' ', 'T');
Then, out of it I'm constructing my date object as follows:
var dateplublished = new Date(dateFormated);
Which gives me the following result :
Wed Apr 26 2017 07:49:09 GMT+0300 (Eastern Europe Daylight Time)
Everything seems to be fine, except that the GMT +0300 won't add. It supposed to spit out :
Wed Apr 26 2017 10:49:09 GMT+0300 (Eastern Europe Daylight Time)
How can I achieve this, so it Would be supported in all major browsers including ios safari etc? I managed to do it before, by doing it like so :
var dateFormated = d.date;
var dateplublished = new Date(dateFormated + " " + "UTC");
This way it would spit out everything perfectly, except that it would show NaN-NaN-NaN on ios safari/chrome.

You can all Z at the end of date. That way new Date(2017-04-26T07:49:09Z); will be resolved to your local time.

Date parsing is best with momentjs.
It's much more reliable and provides cross-browser support.
Then you can use this to achieve what you're trying to do: https://momentjs.com/docs/#/manipulating/local/

Related

Wrong date with angular material's date picker

I use the datepicker to pick a date and send it to the server.
When I log the JS value I get the correct result:
Tue Mar 22 2016 00:00:00 GMT+0100 (Mitteleuropäische Zeit)
but in the ajax request it is
2016-03-21T23:00:00.000Z
I don't modify the values, just giving the object to angulars http function.
Does Angular need some configuration to handle it?
You can try the following piece of code
dateObj.setMinutes((dateObj.getMinutes() + dateObj.getTimezoneOffset()));
No need of localization, use this code just before doing any service call. It will pass you the exact date what you selected in the datepicker.
It will work in all timezone (+) and (-),
Example: 2016-03-21 00:00:00 GMT+0100, the above said code covert it as 2016-03-21 01:00:00 GMT+0000. While on Service it converts it as 2016-03-21 00:00:00.
I think it will solve your problem.
Those two strings represent the same time. One is in UTC, i.e. GMT +0, which you can see from the Z ending. The other is in a different timezone, specifically GMT +1 hour.
If you had javascript date objects for both strings, and turned them into integers, i.e. seconds passed since Jan 1, 1970, UTC, you'd find them identical. They represent the same instant but in two different geographic locations.
var d1 = new Date('Tue Mar 22 2016 00:00:00 GMT+0100');
var d2 = new Date('2016-03-21T23:00:00.000Z');
Number(d1); // 1458601200000
Number(d2); // 1458601200000
Generally this is a good thing. Dealing in timezones gets very confusing. I find it best for a server to always deal in UTC.
https://github.com/angular/material/pull/9410
Check out the 1.1.1+ version. This will solve your issue.
<md-datepicker ng-model="date" ng-model-options="{ timezone: 'utc' }"></md-datepicker>
If suppose am selecting a date like Tue Aug 06 2019 00:00:00 GMT+0530 (India Standard Time), am getting 2019-08-05T18:30:00.000Z. ( which in my case previous date with respect to the selected date)
I made use of toLocalDateString() to do the job.
// this.date = 2019-08-05T18:30:00.000Z
const converted = new Date(this.date).toLocaleDateString();
console.log(converted); // 8/6/2019 (MM/DD/YYYY) format

Change timezone of date in Javascript but NOT the date or time

I have a problem here. I need to change the timezone of of a date in Javascript to UTC before passing it to the back end pf my service for validation. I cannot find a solution in any of the questions on this site or on other sites. The problem is that every method I have tried so far is also changing the time and date of my Javascript date object. For example:
var startDate = new Date($("#start-date-picker").val()).toUTCString();
Changes the time which affects the date (the timezone of my laptop is currently set to GMT +2).
While debugging my date object looks like this:
var startDate = new Date(getProperDate($("#start-date-picker").val()));
//startDate = Wed Aug 26 2015 00:00:00 GMT+0200 (Romance Daylight Time) {}
But when changed using the .toUTCString() method the date ends up like this:
startDate2 = "Tue, 25 Aug 2015 22:00:00 GMT"
I cannot find a way to change just the timezone and preserve the current date and time. I cannot use any external libraries either before anyone suggests moment.js or any others. Any help would be much appreciated, thanks!

How do I prevent javascript from converting my dates to GMT?

I have a timestamp given by
timestamp = 2015-02-22T10:00:00.000Z
Why is it converted to GMT when I do this
var dt = new Date(timestamp);
console.log('dt = ' + dt); // prints Sun Feb 22 2015 05:00:00 GMT-0500 (EST)
I don't want it to convert my date to GMT. How do I prevent javascript from converting my dates?
When you try to execute dt = ' + dt, Javascript tries to convert the dt object to a string so it can be added to another string. It does that by calling the dt.toString() method and the format you are seeing is the default string conversion for a date object.
FYI, this default format that looks like this:
Fri Mar 06 2015 19:24:42 GMT-0800 (Pacific Standard Time)
is NOT GMT time. The time value shown is local time. It is showing you that local time shown is -0800 hours from GMT, but the time itself is expressed in local time.
It's not uncommon to want to just truncate off the last part of this and display:
Fri Mar 06 2015 19:24:42
That can be done like this:
console.log('dt = ' + dt.toString().replace(/\sGMT.*$/, ""));
Working demo: http://jsfiddle.net/jfriend00/hg5m0r1r/
If you want something different to show, then you should construct the string representation you want yourself rather than letting the system automatically call .toString(). You can look at the Date object methods available and decide what you want to display. A Date object internally is a number of ms since the epoch time so any string representation is a conversion of some kind. You have to tell it what conversion you want.
You can see a list of the many date methods here.

datetime in Javascript alert more 2 GMT hours

Working with datetime in Javascript(apache/php/js project) and making alert for debugging I get message like "Sun Nov 30 2014 02:00:00 GMT+0200 ",
though in var time was not specified at all. That is gvfery confusing...
Which is the best and safe way to get rid of this GMT 2 hours ?
Use .toISOString(), e.g:
var myDate = new Date().toISOString();
For older browsers, which don't natively support this method, the function definition can be found here: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toISOString

Inconsistencies when creating new date objects

I am creating new date objects in javascript and seeing some inconsistencies depending on whether I use the dateString parameter vs the year/month/day integer parameters.
Here's an example:
var dt1 = new Date(1979,3,5);
var dt2 = new Date('1979-04-05');
jsFiddle with example
dt1 is assigned the value: Thu Apr 05 1979 00:00:00 GMT-0500 (Central Daylight Time)
dt2 is assigned the value: Wed Apr 04 1979 19:00:00 GMT-0500 (Central Daylight Time)
Can someone explain this behavior? The second example (dt2) happens to be the format that Chrome is returning a selected date from input[type=date] elements which is why I'm trying to figure this out.
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). The form new Date(1979,3,5); is interpreted as local time. You can use Date.UTC to force UTC time for the 3-argument form (see docs).
Date parsing (and timezone handling in particular) is generally not uniform across browsers, and it's better not to depend on it - use UTC whenever possible, or use a separate library like Date.js or moment.js.

Categories