-2

I have a string that looks like 5/27/2015 4:49:54 AM

I need it to be in this format: 2015-27-05T04:49:54+08:00

I tried converting it like so but it throws an error:

var convertedDate = DateTime.ParseExact(originalDate, "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK",
                            new CultureInfo("en-US", true));

I also tried converting it like this but it doesn't seem to do anything, convertedDate ends up being the same as originalDate:

var convertedDate = String.Format("{0:u}", originalDate);
Erica Stockwell-Alpert
  • 4,624
  • 10
  • 63
  • 130
  • You have a string `"5/27/2015 4:49:54 AM"` and use `"yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK"` as format string in `ParseExact`? Of course that doesn't work. – Tim Schmelter May 23 '16 at 14:23
  • You need to supply the format that you want to parse *from*, not to. That gives you a DateTime. However, you appear to need to add an offset, so the [DateTimeOffset](https://msdn.microsoft.com/en-us/library/system.datetimeoffset%28v=vs.110%29.aspx) type might be more useful. Or maybe [Noda Time](https://github.com/nodatime/nodatime) would be easier to use. – Andrew Morton May 23 '16 at 14:24
  • @TimSchmelter saying "Of course that doesn't work" isn't in any way helpful. – Erica Stockwell-Alpert May 23 '16 at 14:26
  • Possible duplicate of [Convert string to Datetime format dd-MMM-yyyy issue](http://stackoverflow.com/questions/37179087/convert-string-to-datetime-format-dd-mmm-yyyy-issue) – Nikhil Vartak May 23 '16 at 14:32

5 Answers5

1

First parse the original string to a DateTime. Then format the DateTime to a string of the desired format.

var originalDate = "5/27/2015 4:49:54 AM";
var result = DateTime.ParseExact(originalDate, "M/d/yyyy h:mm:ss tt", CultureInfo.CurrentCulture)
    .ToString("yyyy'-'MM'-'dd'T'HH':'mm':'sszzzz");
Console.WriteLine(result);

Outputs

2015-05-27T04:49:54-04:00

Note that that the timezone offset you get will be dependent on the culture of the machine this runs on because it is not specified in the original string.

juharr
  • 31,741
  • 4
  • 58
  • 93
1

You need to parse first, then use your desired format string with ToString translate.

Try this:

string input = "5/27/2015 4:49:54 AM";
DateTime originalDate = DateTime.Parse(input);
string output = originalDate.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK");

You can swap out DateTime.Parse with DateTime.ParseExact if needed. DateTime.Parse will attempt to parse using your system culture. You can be more specific if you need/want to be.

Also, keep in mind that the f in the format string creates a mandatory decimal. To match your prescribed output, you should use F or omit.

https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

Kevin Burdett
  • 2,892
  • 1
  • 12
  • 19
1

Try this code.

string inputString = "05/27/2015 04:49:54 AM";

DateTime dt = DateTime.ParseExact(inputString, "M/dd/yyyy H:mm:ss tt",
System.Globalization.CultureInfo.InvariantCulture);

string outputString = dt.ToString("yyyy-MM-ddTHH:mm:sszzz");

Output string will return the value as "2015-05-27T04:49:54+08:00".

Anish Patel
  • 84
  • 1
  • 4
0

Try convertedDate.ToString("yyyy-MM-ddThh:mm:sszzz"), date and time settings on your local machine may affect how the output is formatted so you can't always rely on the in-built formatting strings.

Jeff Watkins
  • 6,343
  • 16
  • 19
0

To parse "5/27/2015 4:49:54 AM" you should use this format string: "M/d/yyyy h:mm:ss tt"

DateTime convertedDate = DateTime.ParseExact(
    "5/27/2015 4:49:54 AM",
    "M/d/yyyy h:mm:ss tt",
    new CultureInfo("en-US", true));

Then you can use DateTime.ToString with the desired format-string which seems to be "yyyy-dd-MM'T'hh:mm:sszzz" to get 2015-27-05T04:49:54+08:00 as output:

string result = convertedDate.ToString("yyyy-dd-MM'T'hh:mm:sszzz");  // zzz to get UTC offset

See: The "zzz" Custom Format Specifier

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939