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?
Member
81 Points
750 Posts
JSON Serialization date format not working
Mar 09, 2017 04:10 PM|mandrews1234|LINK
So I have a class I created to give me a specific date format when I serialize an object.
Then I have a property like so
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
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.
All-Star
17602 Points
3510 Posts
Re: JSON Serialization date format not working
Mar 10, 2017 05:53 AM|Chris Zhao|LINK
Hi Mandrews1234,
If you want to store all timezone information on the server you can:
1. Declare your WEB API 2 Date types with the "DateTimeOffset" type.
2. Finally store your dates within the database using the "datetimeoffset" type.
reference: https://msdn.microsoft.com/en-us/library/bb630289.aspx
Best Regards,
Chris
Member
81 Points
750 Posts
Re: JSON Serialization date format not working
Mar 10, 2017 02:42 PM|mandrews1234|LINK
I found the solution, if I set base.DateTimeStyles = DateTimeStyles.AssumeLocal; in my IssueDateFormat class, that fixes it.