Wrong serialization of timedate in MVC - javascript

In my app, I am using MVC architecture. My controller returns data about the client such as birthdate. Client info is converted into a JSON object and passed to view.
Now the problem is my DateTime field is acting little strange. My date passed from the view is correct:
{29.07.1978 0:00:00}
After conversion to json it is:
/Date(270511200000)/
Now comes the strange part - it returns
28.07.1978
When I try to look for more detail by converting the json date to Date myself by this:
var date = new Date(parseInt(self.FormData.Person.BirthDate().substr(6)));
I get
Fri Jul 28 1978 23:00:00 GMT+0100
But when I enter some newer Date - for example, 1.1.2000, I get a correct date of 1.1.2000.
For some other dates, I'm getting
Fri Jul 28 1959 22:00:00 GMT+0200
Any idea why is this happening? My timezone is GMT+1.
Thanks a lot for any input :)

As per Jonas Wilms answer below - `
Cause summer time was introduced in your country in 1979
timeanddate.com/time/zone/czech-republic/prague
What fixed this for me was
if (birthYear <= 1979) {
if (self.FormData.Person.BirthDate() != null) {
self.FormData.Person.BirthDate(moment(self.FormData.Person.BirthDate()).utcOffset(2).format('DD.MM.YYYY'));
}
}
else {
if (self.FormData.Person.BirthDate() != null) {
self.FormData.Person.BirthDate(moment(self.FormData.Person.BirthDate()).format('DD.MM.YYYY'));
}
}

Related

How to compare dates on UI to dates stored in MySQL database in Cypress JS test?

In my Cypress test, I am trying to compare a date value retrieved from a MySQL DB to the date appearing on the UI.
Here is my assertion:
cy.compareDates(result[0].publishdate, companyDetails.publishedDate())
result[0].publishdate is the value retrieved from the database - 2022-02-14 16:45:37 (expectedDate below).
companyDetails.publishedDate() is the UI element that contains the text 14-Feb-2022 (actualDate below).
Here is my compareDates() function:
Cypress.Commands.add('compareDates', (expectedDate, actualDate) => {
actualDate.then(date => {
const reformattedDate = new Date(date.text())
cy.log('reformat: ' + reformattedDate)
cy.log('ISO string: ' + reformattedDate.toISOString().split('T')[0])
// expect(reformattedDate.toISOString().split('T')[0]).to.equal(expectedDate.split('T')[0])
})
})
The assertion that I have commented out returns this failure:
And here are the values being used:
Reformatted Date: Fri Sep 11 2015 00:00:00 GMT+0100 (British Summer Time)
ISO string: 2015-09-10
What is strange is that the function is working with the below dates:
Reformatted Date: Wed Feb 28 2007 00:00:00 GMT+0000 (Greenwich Mean Time)
ISO string: 2007-02-28
Going back to the original question
In my Cypress test, I am trying to compare a date value retrieved from a MySQL DB to the date appearing on the UI.
The value appearing on the UI is 14-Feb-2022.
The value in the database is 2022-02-14 16:45:37
since they are both strings (from comments), you can compare them with less hassle using dayjs.
Basically, dayjs is really good at parsing date strings into date objects (however the documentation is a bit on the brief side)
For example:
const dayjs = require('dayjs')
it('tests my dates', () => {
const dateFromUI = dayjs('14-Feb-2022')
const dateFromDb = dayjs('2022-02-14 16:45:37')
console.log(dateFromField, dateFromDb)
})
You get objects back from dayjs(myDateString) that have properties for each of the parts of the date.
So if you want to compare year, month, day and ignore time
expect(dateFromUI.$y).to.eq(dateFromDb.$y)
expect(dateFromUI.$M).to.eq(dateFromDb.$M)
expect(dateFromUI.$D).to.eq(dateFromDb.$D)
There are other ways to do the comparison, for example $d property has a string representation Mon Feb 14 2022 16:45:37 and you can do a substring comparison to ignore the time portion.
Or there are diff methods, for example:
dateFromUI.isSame(dateFromDb, 'day') // returns true

Javascript Date creation difference - in IE vs chrome

I am getting a long from the backend system that represents a date.
I convert the long to javascript Date by using - new Date(long).
I get different dates for the same long in IE and Chrome.
The correct date is the one shown in Chrome.
example
For example:
new Date(1032382800000)
In IE the date value is - Wed Sep 18 2002 23:00:00 GMT +0200.
In Chrome date value is - Thu Sep 19 2002 00:00:00 GMT +0300.
How can I solve this discrepancy?
I have found a workaround.
Because the time is not relevant in those date objects, I run the following code:
var iTimezoneOffset = dDate.getTimezoneOffset() - (new Date()).getTimezoneOffset();
if (iTimezoneOffset != 0) {
dDate.setMinutes(dDate.getMinutes() + iTimezoneOffset);
}

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

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.

Json Date to JavasScript Date Conversion problem

Heres the code for converting the json retured date to a string of date.
String.toDate = function(stringDate) {
var newDate = new Date(parseInt(stringDate.replace("/Date(", "").replace(")/", ""), 10));
return newDate;
}
Here are the details:
Date from the database: 2009-11-18 03:23:25.107
Date Returned by JSON: "/Date(1258514605107)/"
Date Returned by the toDate function :
Wed Nov 18 2009 11:23:25 GMT+0800 (Taipei Standard Time)
Web Server and Database server time zone are the same.
Im wondering why the date becomes the current date in my timezone.
Is there anyone here encountered this kind of problem?
and what about your browser/OS settings ? you need the GMT time ?
I think you get get it with toUTCString a more complete reference about the date class
alert(newDate.toUTCString());

Categories