2

C# System.DateTime.Now gives me date with time , but it gives me format whatever i have set in windows calander i.e (a.m.)(p.m.) or even (xyz), how to get standard time format (AM/PM) regardless of customized time format set in windows calander?

Ayazz
  • 53
  • 7
  • 3
    DateTime is just that, its doesnt have "format" specifically - if you want it to show in a format you are doing something with it... – BugFinder May 11 '16 at 13:17
  • The way the date is represented has nothing to do with its value. – Alessandro D'Andria May 11 '16 at 13:17
  • 1
    http://stackoverflow.com/a/7875351/2490247 – Movsar Bekaev May 11 '16 at 13:18
  • If you want a standard format, create an extension method for `DateTime` objects that uses the ToString() method and supply your own format. [This link](https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx) shows the characters used for formatting. – Brian Payne May 11 '16 at 13:20
  • @To_ALLtry to change windows time AM/PM format to custom values like xyz, then check with System.DateTime.Now... it will give you mm/dd/yyyy hh:mm:ss xyz – – Ayazz May 11 '16 at 13:59

2 Answers2

3

System.DateTime.Now doesn't have any presentational format, it's the ToString() that formats it.

Like:

System.DateTime.Now.ToString("dd, MM");

check msdn for more info: https://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
  • try to change windows time AM/PM format to custom values like xyz, then check with System.DateTime.Now... it will give you mm/dd/yyyy hh:mm:ss xyz – Ayazz May 11 '16 at 13:58
2

If you use the DateTime as DateTime type then it will automatically show you the format of your windows. For the custom format, you have to cast it as string

string newdate = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);

useful link MSDN

Mostafiz
  • 7,243
  • 3
  • 28
  • 42
  • rehman your solution works fine as it shows me proper AM.PM format while geting DateTime (isrespective of i have set as xyz instead of AM, or abc instead of PM). but CultureInfo.InvarinatCuture is independent of culture values? or somewhere it can collide with cuiltural specified formats? – Ayazz May 11 '16 at 13:55
  • The invariant culture is culture-insensitive; it is associated with the English language but not with any country/region, take a look at here https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.invariantculture(v=vs.110).aspx – Mostafiz May 11 '16 at 14:15
  • i have read that article, just wanted to ask your experience with CultureInfo thing, thanks though – Ayazz May 11 '16 at 14:19
  • in my experience yes its culture independent and very flexible for variant culture use, in many of my project I have successfully implemented this and outcome is awesome – Mostafiz May 11 '16 at 14:22
  • I have one little problem, if I even assign DateTime Custure.Invariant value to DataTable column of DateTime , then I get the value of that column in culture specific format? is there not any way datetime column in datatable should store my culture.invariant value? – Ayazz May 12 '16 at 11:45
  • yes it is in invarient culture but when you are displaying data from your pc then it always show as your culture, if you see from other culture then it will show using that culture, it not problem for storing date time, its natural and ok – Mostafiz May 12 '16 at 15:49
  • thanks Mostafizur, got the same response from jon skeet aswell – Ayazz May 13 '16 at 12:58