views:

91

answers:

7

I dont get following desired output from a DateTime-Object:

2010-03-29 13:15:00

My default localization is de-DE, so a simple ToString gives me:

29.03.2010 13:15:00

What I've tried is to create a DateTimeFormatInfo Object from US-Culture and use its DateTimePatterns but without success:

Dim usDateFormat As Globalization.DateTimeFormatInfo = New Globalization.CultureInfo("en-US", False).DateTimeFormat
usDateFormat.DateSeparator = "-"
ruleResult.Claim.Last_Updated_Date.ToString(usDateFormat.ShortDatePattern)

Output: 3.29.2010

What is the best way (readable,fastest) to get my desired DateTimeFormat(i need sometimes the short Date and sometimes the Date and Time like in my example above)?

Thanking you in anticipation, Tim

EDIT: why does my DateTimeFormatInfo approach not work and makes it a performance difference when using the String.Format or ToString("yyyy-MM-dd HH:mm:ss") solutions instead of a private shared(static) DateTimeFormatInfo-Object? I'm calling this function at least 30k times and there are many Dates to format inside.

A: 
string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now)

for predefined pattern check this: http://alexonasp.net/samples/stringformatting/

Florian
+1  A: 
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")

DateTime.Now.ToString("yyyy-MM-dd")
Alex Reitbort
+1  A: 

You can use the formatting option in the ToString of the DateTime like this

long

DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

short

DateTime.Now.ToString("yyyy-MM-dd");
w69rdy
+4  A: 

To get 2010-03-29 13:15:00 I would just use a custom pattern. In this case:

DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");

See this MSDN article for more info

Andrew Cox
Thank you (and others). Why does my DateTimeFormatInfo approach with setting the DateSeparator not work and is this the fastest way(performance) to format a Date(see my edit).
Tim Schmelter
I think (as I have not tried it) you want to go for a culture invariant and then change the settings.
Andrew Cox
+7  A: 

I think what you are looking for is:

DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

Here are some examples, I think if look at then you can understand how it works:

// create date time 2008-03-09 16:05:07.123
DateTime dateTime = new DateTime(2008, 3, 9, 16, 5, 7, 123);

dateTime.ToString("y yy yyy yyyy");  // "8 08 008 2008"   year
dateTime.ToString("M MM MMM MMMM");  // "3 03 Mar March"  month
dateTime.ToString("d dd ddd dddd");  // "9 09 Sun Sunday" day
dateTime.ToString("h hh H HH"    );  // "4 04 16 16"      hour 12/24
dateTime.ToString("m mm"         );  // "5 05"            minute
dateTime.ToString("s ss"         );  // "7 07"            second
dateTime.ToString("f ff fff ffff");  // "1 12 123 1230"   sec.fraction
dateTime.ToString("F FF FFF FFFF");  // "1 12 123 123"    without zeroes
dateTime.ToString("t tt"         );  // "P PM"            A.M. or P.M.
dateTime.ToString("z zz zzz"     );  // "-6 -06 -06:00"   time zone
Leo Nowaczyk
This is a serious overkill. It works though.
Alex Essilfie
+2  A: 

How about this solution:

// Get a default DateTimeFormat
var dateTimeFormat = CultureInfo.GetCultureInfo("en-US").DateTimeFormat;
// Clone it to make it writeable
dateTimeFormat = (DateTimeFormatInfo)dateTimeFormat.Clone();
// Change the DateSeparator
dateTimeFormat.DateSeparator = "-";
// Output it as normal
var output = DateTime.Now.ToString("g", dateTimeFormat);

I think what the most people don't know you can get a writeable version of a culture by creating a new object by calling the Clone() method.

Update

As Tim said, you can also call it this way:

// Get a default DateTimeFormat
var dateTimeFormat = new CultureInfo("en-US").DateTimeFormat;
// Change the DateSeparator
dateTimeFormat.DateSeparator = "-";
// Output it as normal
var output = DateTime.Now.ToString("g", dateTimeFormat);

This works, cause we created a new CultureInfo derived from a given culture. The reason why the first one is read-only, comes from the fact, that in the first case we'll get a reference to some statically created instance somewhere deep down in the framework and with the Clone() call we create a fresh class with the same settings. In the second approach we create directly the fresh class, but say we'd like to get all the settings out of a specific culture.

So at the end of the day, both versions are doing the same thing just described in other words. If there is a performance difference between both versions i can't tell, but i think this can be neglected.

Update 2

After reading Toms last comment he tries something like this:

// Get a default DateTimeFormat
var dateTimeFormat = new CultureInfo("en-US").DateTimeFormat;
// Change the DateSeparator
dateTimeFormat.DateSeparator = "-";
// Output it as normal
var output = DateTime.Now.ToString(dateTimeFormat.ShortDatePattern);

Which leads to a wrong output. This comes, because within dateTimeFormat.ShortDatePattern is just a normal string containing M/d/yyyy. So it is the same as calling DateTime.Now.ToString("M/d/yyyy").

Due to the fact, that the / will be replaced by the DateSeparator and currently no IFormatProvider is given it will take the current culture which is in your case german. So to solve this problem you should try the following code:

// Get a default DateTimeFormat
var dateTimeFormat = new CultureInfo("en-US").DateTimeFormat;
// Change the DateSeparator
dateTimeFormat.DateSeparator = "-";
// Output it as normal
var output = DateTime.Now.ToString(dateTimeFormat.ShortDatePattern, dateTimeFormat);

And with these informations you should also be able to make some assumptions about what is faster:

var output = DateTime.Now.ToString(dateTimeFormat.ShortDatePattern, dateTimeFormat);
var output = DateTime.Now.ToString("M/d/yyyy", dateTimeFormat);

Due to the fact that these are exactly the same there should be no difference in performance

Oliver
Thanks. And i forgot to mention that i got an InvalidOperationException because it is Readonly, but only when using the GetCultureInfo function instead of the constructor. Whats the difference?
Tim Schmelter
And why is the DateSeparator output still '.' instead of '/' even though it should be the slash according to the debugger?
Tim Schmelter
@Tim: I tested the code above with `@"\"`, `"\t"`, "_" and "/". In every case i got the expected result into my `output` variable. The only what i can think of is that you missed to add the `dateTimeFormat` as second parameter in your `ToString()` call.
Oliver
@Oliver: thanks for your investigations. Date.Now.ToString(dfi) gives the expected result(where dfi is a datetimeformatinfo-object with dateSeparator="-"). But Date.Now.ToString(dfi.ShortdatePattern) gives the wrong result with "." as separator. But anyway, i think i use the "yyyy-MM-dd hh:mm:ss"-approach, even if i think this is less readable and perhaps(?) slower.
Tim Schmelter
A: 

you can also try format(date.now,"yyyy-MM-dd HH:mm:ss)

Besnik