Solved

C# DateTime format

Posted on 2016-08-25
5
60 Views
Last Modified: 2016-09-12
Hi all,

Can we represent DateTime in C# in below format. I haven't found this format in .Net

2016-08-31T15:00:00-04:00

Here is -4 is UTC offset.  The time is local EST time

 I want to serialize the DateTime to Json in the above format. How do we convert to this format.

Thanks in advance
0
Comment
Question by:Member_2_7967608
  • 3
5 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41771086
Assuming that the DateTime is from a DateTime object you can use the ToString to get it in that format like this
var dt = DateTime.Now;
Console.WriteLine(dt.ToString("yyyy-MM-ddTHH:mm-ss:ffff"));

Open in new window

0
 
LVL 2

Assisted Solution

by:Kyaw Wanna
Kyaw Wanna earned 500 total points
ID: 41771226
Please do the following steps to convert into json serialization.

 - Please use Newtonsonft.Json and Newtonsoft.Json.Converters to serialize JSON format.

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

Open in new window


- Create Model Class for converting to Json Serialization

 
public class clsDateTime
{
      public DateTime attrDateTime { get; set; }
}

Open in new window


 - Convert UTC time to Eastern Time Zone

 
//--- Use this code in your program for converting to EST Time ----
var utcTime = DateTime.UtcNow;
TimeZoneInfo estZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime estTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, estZone);

Open in new window


 - Convert to JSON format using Newtonsoft.Json
 
//--- Convert To Json Serialize Format
clsDateTime objDatetime = new clsDateTime { attrDateTime = estTime };
string isoJson = JsonConvert.SerializeObject(objDatetime, new IsoDateTimeConverter());

Open in new window

0
 

Author Comment

by:Member_2_7967608
ID: 41771759
This is my code. How can I set the formatter here.  The datetime is coming from UI as "req2".


 var request = new RestRequest("api/ACSSchedule/", Method.POST) { RequestFormat = DataFormat.Json };
            request.JsonSerializer = new RestSharp.Newtonsoft.Json.NewtonsoftJsonSerializer();
            request.AddBody(req2);
            var response = Execute(request, _client);
0
 

Accepted Solution

by:
Member_2_7967608 earned 0 total points
ID: 41771852
I think NewtonJson serializer is able to format in yyyy-MM-ddTHH:mm-ss:zzz

i.e. represnted as

2016-08-31T15:00:00-04:00
0
 

Author Closing Comment

by:Member_2_7967608
ID: 41793918
Formatting works with Newtonsoft Json serailizer
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question