-5

How I can format a DateTime in C# like this 2015-09-24T09:30:00+05:30?

I found a method like this date.ToString("o");, but which is formatting the string like this 2015-10-05T09:30:00.0000000+05:30. Adding the additional .0000000 to the string.

Any one please help to get this corrected.

Wesley Lomax
  • 2,067
  • 2
  • 20
  • 34
StezPet
  • 2,430
  • 2
  • 27
  • 49
  • See [Custom Date and Time Format Strings](https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx) – juharr Sep 23 '15 at 16:39
  • @juharr I suspect that the format sought is available using a [Standard Date and Time Format String](https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx) but that `"o"` is just not the right one. – phoog Sep 23 '15 at 16:41
  • check http://stackoverflow.com/questions/114983/given-a-datetime-object-how-do-i-get-a-iso-8601-date-in-string-format – V4Vendetta Sep 23 '15 at 16:42
  • What's with all the downvotes?? – Arian Motamedi Sep 23 '15 at 16:54
  • @PoweredByOrange I did not vote, but I suspect very strongly that the downvotes reflect the apparent lack of any effort on the part of the OP to locate the `ToString` documentation or to do any other research to solve the problem. – phoog Sep 23 '15 at 18:07

1 Answers1

3

There aren't any shorthand options but this should do the trick.

date.ToString("yyyy-MM-ddTHH:mm:sszzz")

Refer to Microsoft's documentation: https://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx

Adam Milecki
  • 1,738
  • 10
  • 15
  • 1
    Note that the `':'` character represents the culture-specific time separator, so the format string given will produce an incorrect result in certain cultures. You can escape the character to force a literal `:`. – phoog Sep 23 '15 at 18:09