Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am in a scenario where date is causing problem. I have a date picker when selected date is of format dd.mm.yyyy (German). I need to then pass to REST API(Sharepoint) where it allows time containing Z (zulu=>UTC). So i converted the date like this

JavaScript
var tmpAuditDate = $(this).parent().find('[id*=Date]').val(); //value from date picker
      var auditDate = "";
      if (tmpAuditDate) {
          var dString = tmpAuditDate.split(dateSeparator);
          if (dateSeparator == ".") {
              auditDate = new Date(dString[2], dString[1] - 1, dString[0]);
          }
          else {
              auditDate = new Date(dString[2], dString[0] - 1, dString[1]);
          }
          auditDate.setDate(auditDate.getDate());
      }


I got the audit date as this "Tue Feb 3 00:00:00 UTC+0530 2015" but when i pass it to REST it throws an error, primitive types are not allowed and when i checked the rest api result its in Edm.DateTime format. So i converted the date using this

JavaScript
auditDate.toISOString()

Although it was saved successfully the date i got the result like this "2015-02-02T18:30:00.000Z", which is a day back.

So how can i achieve this.

Also i saw an implementation of ISOString convertion if browser doesnot support the function as specified here ISO

But where shall i put this???

Thanks in advance
Arjun Menon
Posted
Updated 27-Jan-15 19:17pm
v3
Comments
Richard Deeming 28-Jan-15 8:14am    
The reason you're getting the previous day is due to time zones - midnight on 3rd Feb in UTC+5:30 is the same as 18:30 on 2nd Feb in UTC.
Arjun Menon U.K 31-Jan-15 0:45am    
thanks richard

 
Share this answer
 
Comments
Arjun Menon U.K 28-Jan-15 5:44am    
Hi Snesh,
Thanks for the reply.I tried with toUTCString and it gave me a format like this "Mon, 5 Jan 2015 08:00:00 UTC" and the REST API returned exception as this "Cannot convert a primitive value to the expected type 'Edm.DateTime'. "
Snesh Prajapati 28-Jan-15 5:52am    
Hi Arjun, You have to work under the constraints given while using an API...It depends what they allowed for API to accept. Similar Issue is faced here too:
http://stackoverflow.com/questions/11923822/timespan-edm-time-format-in-odata
and
http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx

Thanks.
Arjun Menon U.K 28-Jan-15 7:46am    
I think i found the solution.
var tmpAuditDate = $(this).parent().find('[id*=Date]').val(); //value from date picker
var auditDate = "";
if (tmpAuditDate) {
var dString = tmpAuditDate.split(dateSeparator);
if (dateSeparator == ".") {
auditDate = new Date(Date.UTC(dString[2], dString[1] - 1, dString[0]));
}
else {
auditDate = new Date(Date.UTC(dString[2], dString[0] - 1, dString[1]));
}
auditDate.setDate(auditDate.getDate());
}
Snesh Prajapati 28-Jan-15 7:51am    
Nice. Thanks.
I think i found the solution.
var tmpAuditDate = $(this).parent().find('[id*=Date]').val(); //value from date picker
var auditDate = "";
if (tmpAuditDate) {
var dString = tmpAuditDate.split(dateSeparator);
if (dateSeparator == ".") {
auditDate = new Date(Date.UTC(dString[2], dString[1] - 1, dString[0]));
}
else {
auditDate = new Date(Date.UTC(dString[2], dString[0] - 1, dString[1]));
}
auditDate.setDate(auditDate.getDate());
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900