Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# DateTime format

Posted on 2016-08-25
5
Medium Priority
?
130 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

715 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