Solved

C# DateTime format

Posted on 2016-08-25
5
45 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 62

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now