-3

My datetime is in this format 2017-01-15T21:00-07:00 so I am parsing it out like so

string ADT = Convert.ToString(timestamp.DateOrTimestamp);
int index = ADT.IndexOf("T");
string FADT = (index > 0 ? ADT.Substring(0, index) : "");
DateTime dtFADT = DateTime.ParseExact(FADT, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
string ADT1 = ADT.Split('T', '-')[3];

Console.WriteLine("{0}: {1}", timestamp.Type, dtFADT.ToString("MM/dd/yyyy") + " " + String.Format("{0:h:mm t}", ADT1));

Which will write to the console: Delivery: 01/15/2017 09:00:00

Why does the String.Format() not add in the AM/PM to my time?

  • `21:00-07:00` is this a range or what? If `07` is for seconds, then what does the last `00` means? – Ghasan غسان Jan 16 '17 at 02:10
  • Check this: http://stackoverflow.com/questions/7875259/how-do-i-get-the-am-pm-value-from-a-datetime and also: http://stackoverflow.com/questions/13044603/convert-time-span-value-to-format-hhmm-am-pm-using-c-sharp – Mrinal Kamboj Jan 16 '17 at 02:11
  • Your `DateTime` is **NOT** in the format `2017-01-15T21:00-07:00`. That is a `string`. `DateTime` does **NOT** have a format - it's just a number. It's only when you call `.ToString(...)` that you create a `string` that has a format. – Enigmativity Jan 16 '17 at 02:14
  • @Enigmativity - sorry for not properly understanding DateTime vs String when it comes to a DateTime or String. How about an applicable example of remedying my issue and not a tongue lashing or lesson on variables. – StarsFlyFree FromCozyNights Jan 16 '17 at 02:16
  • You can have a look at [ParseExact](http://stackoverflow.com/a/41606677/3796048) and look for `format` you can also set accordingly for you. – Mohit S Jan 16 '17 at 02:19
  • @Ghasan - no it is in a String Format (not sure why they call it DateOrTimestamp if it is string) – StarsFlyFree FromCozyNights Jan 16 '17 at 02:20
  • @MohitShrivastava - that got it. If you put as answer I accept. – StarsFlyFree FromCozyNights Jan 16 '17 at 02:21
  • @StarsFlyFreeFromCozyNights Have a look at the updated answer. :) – Mohit S Jan 16 '17 at 02:39
  • 1
    @StarsFlyFreeFromCozyNights - Sorry, there was no tongue lashing. Just trying to be very clear about the relationship between `DateTime` and date/time formats. – Enigmativity Jan 16 '17 at 04:17

2 Answers2

1

This might do the trick for you

string smdt = "2017-01-15T21:00-07:00";
string format = "yyyy-MM-ddTHH:mmzzz";
DateTime dt = DateTime.ParseExact(smdt, format, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
string extractedDate = dt.ToString("dd/MM/yy h:mm tt");
Mohit S
  • 13,723
  • 6
  • 34
  • 69
0

To do something similar I ended up having to make a CultureInfo based off eu-US but with the 2nd argument set to false to not allow system / user over-rides of the formats.

public static CultureInfo enUS = new CultureInfo("en-US", false);

Then I could use it in code anywhere I needed to format a date for parsing or toString with a date/time having the tt part of the format it would put the AM or PM that I wanted.

The system language setting on the PC the software was running on was different to mine. They used a.m or Pm or something like that. when we wanted to use AM or PM.

Liam Mitchell
  • 1,001
  • 13
  • 23