Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# DateTime format

Posted on 2016-08-25
5
Medium Priority
?
146 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 64

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 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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 article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Integration Management Part 2
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

824 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