0

I'm working on an application where I'm sending datetime from JavaScript (client side) to a Web Service (server side). Now problem with DateTime is it has many formats and at any instance client might have a different format of DateTime than of server, which might break the parsing of datetime on server side.

I was thinking may be JavaScript's function "getTime()" will be an equivalent of C#'s datetime property "Ticks", so that I can sent getTime() from front end and can easily parse it to valid DateTime on server end. But unfortunately that doesn't seems to be the case :(

So is there any universal format that I can use for DateTime that would take my worries away of client's format being different and server responding with 500?

UPDATE

I can get into practice of sending "YYYY-MM-DD" or any other pre-defined format from front end and parse accordingly on back end, but it's viable only till someone misses it, and as a project gets bigger and more devs starts working on it, practices like this becomes overhead on management. So in short it is a work around but not a bull's eye solution. Thanks Mohit for bringing it up I forgot to mention.

yogi
  • 19,175
  • 13
  • 62
  • 92
  • `YYYY-MM-DD` is what will take most of your worries – Mohit S Nov 25 '15 at 08:20
  • 1
    `getTime` returns milliseconds since `1970/01/01 00:00:00 UTC`. `Ticks` returns ticks from `01/01/0001 00:00:00 UTC`. They are quite different as you can see. Can you please show your work by the way how to send it from client side to server side? – Soner Gönül Nov 25 '15 at 08:20
  • UTC Datetimes is the best option I found so far – Domysee Nov 25 '15 at 08:24
  • Possible duplicate of [How to convert Javascript datetime to C# datetime?](http://stackoverflow.com/questions/6702705/how-to-convert-javascript-datetime-to-c-sharp-datetime) – mortb Nov 25 '15 at 08:26
  • Why does the format matter? You control both the client and the server application, so you can define the format and stick to it. – Domysee Nov 25 '15 at 08:27
  • `YYYY-MM-DD` is required by the the web profile of ISO 8601. There'd be a problem if somebody decided to use something else, but there'd be exactly the same problem if somebody decided to use `` rather than `

    ` in HTML, and the solution would be the same too.

    – Jon Hanna Nov 25 '15 at 11:19

2 Answers2

1

I'd suggest the following:

  • Use JavaScript UTC clientside to send up to the server http://www.w3schools.com/jsref/jsref_utc.asp Or use a date format that cannot be confused (i.e. Long date or "yyyy-MM-dd")
  • On the server store the dates in UTC
  • When sending dates clientside send UTC dates to the client and use a JavaScript library like http://momentjs.com/ to render dates clientside in the client's time zone.
Garth
  • 533
  • 1
  • 4
  • 15
0

in my option , this is not pertain to time format or team convention or other something .

The real question is why you handle time with "String" , not "Date", you get a Date object ,and turn it to String object , do some operation with string(what is boring and dangerous),and then turn it as Date() (or DateTime in C#) back .

string is string , time object is time object .

the only moment we do date=>string action is showing to end user , as possible as,we handle it by time object and use some stable tools to translating

for example: we have a dateTime object in c#,and we response it to client, this is its format,the most standard format :

CreateTime: "/Date(-62135596800000)/" 

and it will be translated as a Date object in js . with this , you don't need care UTC or local ,yyyy-MM-dd or yyyy/MM/dd . with some date lib , you can do anything to it as you want in a standard base line . after your all strange operation ,it is still a date object ,still a standard format,and transport it to service side ,it will be a DateTime object(still a standard format) in C# .

if you need get date from some other source like user input, No matter what you want to do next,first and first translate it to a date object.

JsonSong
  • 328
  • 2
  • 14