Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

JSON Serialization date format not working

$
0
0

So I have a class I created to give me a specific date format when I serialize an object.

	public class IssueDateFormat : IsoDateTimeConverter
	{
		public IssueDateFormat()
		{
			base.DateTimeFormat = "yyyy-MM-dd HH:mm:ss K";
		}
	}

Then I have a property like so

[JsonConverter(typeof(IssueDateFormat))]
public DateTime issued_at { get; set; }

Now, if I set obj.issued_at to Datetime.Now I get the proper date format 2017-03-09 09:58:31 -06:00 in the json string. However, if I set it to my database datetime value, I get 2017-03-09 09:58:31 

Any ideas why when it takes a date from the database, it doesn't add the -06:00 on the end?

Edit

I want to add I just discovered that if I do

DateTime dt = new DateTime(exam.exam_date_time.Year, exam.exam_date_time.Month, exam.exam_date_time.Day, exam.exam_date_time.Hour,
					exam.exam_date_time.Minute, exam.exam_date_time.Second, exam.exam_date_time.Millisecond, DateTimeKind.Local);

badge.issued_at = dt;

Then I do get the -06:00 on the end, my question is, how could I handle that in the property in my object class so that it automatically does that. 


Viewing all articles
Browse latest Browse all 4850

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>