-4

I need help to convert current date to "Tue Nov 4 00:00:00 UTC+0530 2014" date format using C#.

Say I have date like : DateTime dt = DateTime.Now;

Now, how can I convert it in mentioned format.

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
Avijit
  • 1,219
  • 2
  • 15
  • 28

3 Answers3

1

DateTime.ToString(string) allows you to specify a format for your date. You can construct a custom format using Custom Date and Time Format Strings.

Jussi Kosunen
  • 8,277
  • 3
  • 26
  • 34
0

I feel like taking risk to answer your question but anyway..

A DateTime doesn't have any implicit format. It just have date and time values etc.. That's why it is not possible to have any format. But string representations of them can have a format. Formatting a DateTime is easy, just need to use DateTime.ToString() method with a specific culture. (In your case looks like InvariantCulture is a good candidate)

DateTime dt = DateTime.Now;
dt.ToString("ddd MMM d HH:mm:ss 'UTC+0530' yyyy", CultureInfo.InvariantCulture);

returns

Tue Nov 4 14:20:36 UTC+0530 2014

AFAIK, time zone abbreviations are not standardized and that's why there is way to parse them besides literal string delimiter. Also since a DateTime doesn't keep offset value, you need also parse it as a literal string delimiter

If you would want to +05:30 instead +0530 as a result, "zzz" custom format specifier would be a nice choice since it gets + or - sign, hours and minutes part offset of local operating system's time zone from UTC.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • If you use `"ddd MMM d HH':'mm':'ss 'UTC+0530' yyyy"` with the colons `:` protected by single quotes, it is not necessary to specify the format provider (culture info), I think. – Jeppe Stig Nielsen Nov 04 '14 at 12:29
  • Note that `"ddd MMM d HH:mm:ss 'UTC'zzz yyyy"` will reflect the time zone of the current machine (the `zzz` part) but there will be a colon between `05` and `30` (assuming Indian/Sri Lankan locale). – Jeppe Stig Nielsen Nov 04 '14 at 12:31
  • @JeppeStigNielsen Yes but if OP's `CurrentCulture` is different than english based culture, `ddd` and `MMM` might generates different abbreviated names as results than `Tue` and `Nov`. – Soner Gönül Nov 04 '14 at 12:31
  • @JeppeStigNielsen You are right about `zzz` format and missing `:` between `05` and `30` (will add to my answer as well). That's why I couldn't find any solution (even if there is another way, I don't know) except literal string delimiter. – Soner Gönül Nov 04 '14 at 12:37
-1

Based upon your suggestions, I build this code

DateTimeOffset localTime = DateTimeOffset.Now;
string.Format("{0:ddd MMM d HH:mm:ss} UTC+{1} {0:yyyy}", localTime, localTime.Offset.ToString("hhmm"));

and its generating correct format: "Tue Nov 4 18:25:48 UTC+0530 2014"

Avijit
  • 1,219
  • 2
  • 15
  • 28
  • @HamidP: r u sure that if change "hhmm" to "HHmm" is going to work. Have u tested it?If I change "hhmm" to "HHmm" then I am getting System.FormatException "Input string was not in a correct format." – Avijit Nov 06 '14 at 20:24