How to write a date in ISO 8601 type

Asked

Viewed 4,858 times

-1

I’m doing a communication program with Apis online and I’m using a header like DateTime and I needed that date to stay on the ISO 8601 how can I do this ?

I’m using this to create the header :

var tempo = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
var httpWebRequest = (HttpWebRequest)WebRequest.Create("teste");
httpWebRequest.Headers.Add("Date"+ tempo);

The output is this:

Date 2017-09-08T15:25:53Z

I wish it was this:

Date: 2017-09-08T15:25:53Z

  • I edited the answer, what you wanted was something else.

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

3 answers

3

DateTime.UtcNow.ToString("o");

or

DateTime.UtcNow.ToString("yyyy-MM-ddTHH\\:mm\\:ss.fffffffzzz");

Source.

Documentation.

This is a form of representation of a text in the specified format. A DateTime has no format, is a date is ready, only texts have format. By the question, including the edition, what you want is a text with the representation and not the date itself which is a quantitative number.

In fact the question is chameleon, what you really want is not the date in ISO format but build the text correctly, so:

httpWebRequest.Headers.Add($"Date: {DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ")}");

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • how can I use this in order to stay in Datetime ?

  • 1

    @Pedroazevedo I edited the answer to show you that this doesn’t make sense.

  • received the following error: System.Argumentexception: 'The 'Date' header has to be modified with the proper property or method.'

  • This is already another mistake, you are greatly modifying what was asked, open another question about this.

  • is an error regarding what you posted in the reply

  • No, your question was about the date format, this was answered and I demonstrated in the answer (until running) that it is as you specified in the question, now you have a problem with the request. This is another completely different problem.

  • thank you very much in the same I will make another question

Show 2 more comments

2

Option 1:

DateTime.UtcNow.ToString("o");

Option 2:

DateTime.UtcNow.ToString("yyyy-MM-ddTHH\\:mm\\:ss.fffffffzzz");

Option 3 (with the specified used format):

DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"); 

Reference

  • how can I use this in order to stay in Datetime ?

  • ISO 8601 is a default display (a date writing format), not a "value" itself. The closest you can get to staying in Datetime is DateTime.UtcNow and at the time the date is displayed, call the ToString equal to the answer.

0

var tempo = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
var httpWebRequest = (HttpWebRequest)WebRequest.Create("teste");
httpWebRequest.Headers.Add("Date: "+ tempo);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.