39

I have a C# DateTime object. This object includes both the date and time. I need to pass this information to a REST-based service. My question is, how do I format the DateTime, such that I can pass it via the query string, and parse it back into a DateTime on the server-side?

DateTime startDate = GetStartDate();
string url = "http://www.mydomain.com/myservice.svc/[startDateGoesHere]

WebRequest request = HttpWebRequest.Create(url);
request.BeginGetResponse(new AsyncCallback(Service_Completed), request);

Thank you,

6 Answers 6

50

Just use ToString() and pass a format e.g.:

startDate.ToString("yyyyMMddHHmmss")

And parse it back by using DateTime.ParseExact()

Sign up to request clarification or add additional context in comments.

2 Comments

DateTime.ParseExact was 'exactly' what I needed. No pun intended. Thanks!
Consider using startDate.Ticks and new DateTime(ticks) also
29

For accuracy and consistency you could use:

string utcDateOut = DateTime.UtcNow.ToString("s", CultureInfo.InvariantCulture);

DateTime utcDateIn = DateTime.ParseExact(utcDateOut, "s", 
                              CultureInfo.InvariantCulture, 
                              DateTimeStyles.AdjustToUniversal);

This will give you an ISO 8601 compliant format and the use of UTC will ensure that there are no issues with time zones etc.

Only drawback is that it doesn't look as "nice" as a simple "yyyyMMdd".

Comments

7

I'd use yyyyMMdd as the format; doesn't need to be URL encoded and it's easy to read/understand.

On the server side, you'd have to call DateTime.ParseExact(dateString, "yyyyMMdd") to get the date out.

Hope this helps.

Comments

4

string s = DateTime.ToString("yyyyMMddHHmmssfff")

Note: fff is milliseconds (you may remove the 'fff' if it is not needed)
Then convert it back to DateTime using

DateTime d = DateTime.ParseExact(s, "yyyyMMddHHmmssfff", CultureInfo.InvariantCulture)

Comments

2

For those wanting to create a date/time in UTC and then have your web api consume it and be aware of UTC as the DateTime kind you can do:

var startDate = DateTime.UtcNow.AddHours(-24).ToString("u",CultureInfo.InvariantCulture);
var endDate = DateTime.UtcNow.ToString("u", CultureInfo.InvariantCulture);

This will output your date time as:

2023-12-07 10:47:04Z

The Z here will be used to convey the datetime is in UTC format. Then on your consuming API, for example:

[HttpGet]
public async Task<IActionResult> GetSomething(DateTime startDate, DateTime endDate)

It will then parse the DateTime in the correct format with the DateTime kind as UTC.

Hope that helps :)

Comments

0

I used the format below in my case. I had no control of the controller side and this format turns it correctly into a DateTime object.

Controller endpoint:

public void GetObjectsCreatedFrom(DateTime createdFrom)
{...}

My test code:

DateTime from = DateTime.Now;
string url: "test/sorted?createdFrom={from:yyyy-MM-ddTHH:mm:ssZ}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.