0

I have a class with the datacontract and datamember attributes applied to its properties, including a datetime, like so:

[DataMember]
public DateTime MyDate{get;set;}

I'm using ADO.NET inside a class the webapi controller uses to get the data from the database, like so

MyDate = Convert.ToDateTime(reader["mydate"]);

Say for instance the date in the database is 2016-01-21 16:30:00.000. When I display it in javascript, ie.

new Date(value.MyDate).toString('MM/dd/yyyy hh:mm:ss')

it shows up on screen as 2016-01-21 04:30:00.000. When I check the fiddler response, the JSON is returning 2016-01-21T16:30:00 for the "MyDate" property of the object. What am I doing wrong? Why isn't this showing up in military time?

user609926
  • 801
  • 2
  • 12
  • 25
  • Possible duplicate of [convert 12-hour hh:mm AM/PM to 24-hour hh:mm](http://stackoverflow.com/questions/15083548/convert-12-hour-hhmm-am-pm-to-24-hour-hhmm) – Mike Cluck Jun 28 '16 at 17:34
  • Why would it be displaying 12-hr dates it 24 hour dates are being returned? If it's incorrectly displaying 12-hr dates I could be converting false negatives into double-digit dates – user609926 Jun 28 '16 at 17:45
  • Because it recognizes that string as a datetime format and converts it into a `Date` object. When you print out a `Date` object, you'll see 12-hr dates. You'll need to manually convert it to a 24-hr format. – Mike Cluck Jun 28 '16 at 17:48
  • If it prints everything out as 12-hr dates how do you determine if a date is supposed to be 04 or 16? – user609926 Jun 28 '16 at 18:05
  • Because I goofed up the last thing I said. Ignore it. The way you're formatting the string is incorrect, which is looks like you figured out. Sorry I led you astray haha. – Mike Cluck Jun 28 '16 at 18:09

1 Answers1

2

Changing the hours to upper case in the date format string, from 'MM/dd/yyyy hh:mm:ss' to 'MM/dd/yyyy HH:mm:ss' corrected the issue.

user609926
  • 801
  • 2
  • 12
  • 25