0

I have DateTime format string '2017-08-18 06:00:00 IST' i want to convert to 'Fri 18th Aug 2017 / 06:00:00 IST'

please help me in this.

Thanks in advance.

  • Aside from anything else, it's unclear whether you're really starting with a string or a `DateTime`. A [mcve] would have helped here, along with how far you've already got. – Jon Skeet Oct 08 '17 at 07:23

1 Answers1

0

Converting a DateTime to String

I'm not clear exactly on if you are intending to do string --> DateTime --> string... But the following solution will do the DateTime to string, and gets you the desired output:

// set a date in UTC format
DateTime dateUtc = new DateTime(2017, 08, 18, 06, 00, 00).ToUniversalTime();

// convert the UTC date to the IST time zone
DateTime dateIST = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dateUtc, "UTC", "India Standard Time");
string timeZoneText = "IST";

// the string you want
string result = String.Format("{0:ddd d}{1} {0:MMM yyyy / HH:mm:ss} {2}", dateIST,
    (DateTime.Now.Day % 10 == 1 && DateTime.Now.Day != 11) ? "st"
    : (DateTime.Now.Day % 10 == 2 && DateTime.Now.Day != 12) ? "nd"
    : (DateTime.Now.Day % 10 == 3 && DateTime.Now.Day != 13) ? "rd"
    : "th", timeZoneText);

The key here is the string format

{0:ddd d}{1} {0:MMM yyyy / HH:mm:ss} {2}

// 0 = DateTime (converted to your time zone, ex: IST)
// 1 = the "1st", "2nd", "3rd", "#th"
// 2 = "IST"

This will result in: Fri 18th Aug 2017 / 06:00:00 IST

Svek
  • 12,350
  • 6
  • 38
  • 69