0

I want to format a DateTime to the following format yyyy-MM-ddTHH:mm:ssZ but keeping it in DateTime.

This is what I tried:

string t = Convert.ToString(DateTime.UtcNow);
DateTime d2 = DateTime.ParseExact(t, "yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);

But I am getting the following error :

System.FormatException: 'The string was not recognized as a valid DateTime value.'

this is the result where I want to get: 2018-09-05T09:29:56Z

How can I solve this problem?

piet.t
  • 11,718
  • 21
  • 43
  • 52
Spiek
  • 11
  • 4
  • Can you give more details about what are you trying to achieve? – Florin-Constantin Ciubotariu Sep 05 '18 at 09:35
  • If you're parsing an ISO8601 date, simple use `DateTime d2 = DateTime.Parse("2010-08-20T15:00:00Z", null, System.Globalization.DateTimeStyles.RoundtripKind);`. – ProgrammingLlama Sep 05 '18 at 09:36
  • Possible duplicate of [How to create a .NET DateTime from ISO 8601 format](https://stackoverflow.com/questions/3556144/how-to-create-a-net-datetime-from-iso-8601-format) – ProgrammingLlama Sep 05 '18 at 09:37
  • 2
    Your code doesn't really make sense. First of all, why do you want to convert to `string` and then back? Can't you keep it as a `DateTime` for the whole time? Second, you shouldn't use `Convert.ToString` here, use `date.ToString` with the same format string you have later. – DavidG Sep 05 '18 at 09:37
  • 2
    By the way, do remember that `DateTime` values only have a format once you've converted them to string. When you convert back to `DateTime`, they are once again stored as `Ticks`. Read as: DateTime objects have no format. – ProgrammingLlama Sep 05 '18 at 09:39
  • _" I am doing an httpWebRequest and need this value in DateTime to insert the Date field in the Header."_ - This is a [x-y-question](https://meta.stackexchange.com/a/66378). The HttpWebRequest API takes a DateTime object : https://learn.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.date?view=netframework-4.7.2 So if you already have a DateTime object - use it. The HttpWebRequest will take care of formatting it correctly. – Fildor Sep 05 '18 at 11:37

2 Answers2

1

No you cannot do that, If your requirement is to display the Date object in localized format, then you can make use of the Overrided .ToString() method like the following:

string formatString = "yyyy-MM-ddTHH:mm:ssZ";
DateTime utcDate = DateTime.UtcNow;
string formatedDate = utcDate.ToString(formatString) 

Read more about Standard Date and Time Format Strings

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • Or just use "o", or "u", depending on your needs. – ProgrammingLlama Sep 05 '18 at 09:39
  • But I need to keep it as datetime but with the format of ISO 8601 – Spiek Sep 05 '18 at 09:47
  • What you are going to do with this DateTime object? where you are keeping this value? @Spiek – sujith karivelil Sep 05 '18 at 09:55
  • @sujithkarivelil I am doing an httpWebRequest and need this value in DateTime to insert the Date field in the Header. – Spiek Sep 05 '18 at 09:59
  • @Spiek: Why not? Save as UTC date object in the Database, whenever you wanted to display the date in local date format, using the formatted string – sujith karivelil Sep 05 '18 at 10:05
  • 1
    @Spiek repeat after me "a datetime object stores only a number, it has no format" – ProgrammingLlama Sep 05 '18 at 10:25
  • _"But I need to keep it as datetime but with the format of ISO 8601"_ You cannot. _DateTime has no format_. The header in a HttpWebRequest will be a string. So if it has an API to add it and that API takes a DateTime, give it the DateTime. If it takes a string, use .ToString with correct format. There is no inbetween. – Fildor Sep 05 '18 at 11:28
  • This [Docu](https://learn.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.date?view=netframework-4.7.2) says it is a DateTime. So you are done. No formatting needed. – Fildor Sep 05 '18 at 11:34
1

DateTime is just a primitive type, and you rather convert it to the format you want.

The format is just a presentation of your DateTime type

string t = Convert.ToString(DateTime.UtcNow); // it gives you a string like 9/5/2018 9:38:40 AM
DateTime d2 = DateTime.ParseExact(t, "yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);

Obliviously, the format you put doesn't correspond with the string t

Try this

//to have the right format
var t = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssZ"); //it gives 2018-09-05T09:44:14Z

//to convert a string with a format given
DateTime.ParseExact("2018-09-09:38:40Z", "yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture)
Antoine V
  • 6,998
  • 2
  • 11
  • 34