2

So this seems like an easy thing to do, but still has me stumped. I want to display a list of strings to my user, based off of a few file's creation dates. So basically, display a list of DateTimes. The challenge is that want to use a custom format (something like 5/6/13 12:01 PM) but I want he date part of that to display differently based on how you have your system displaying the date (ie. a Brit would display that date as 6/5/13).

I thought I could just build two strings (one for date and one for time) and make sure that they date is region-formatted, but there is no default option for 5/6/13 (only 5/6/2013): http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

Next I hoped maybe the DateTime.ToShortDateString() function would work, but it displays as 5/6/2013 as well.

I know I can use a completely custom format like this: DateTime.ToString("M/d/yy h:mm tt") but I don't want to fix the date with the month before the day.

I suppose if I can' figure anything out then I could just build a custom datetime for America and for Europe and then query the OS for what datetime they are displaying in. But that seems really excessive. Any thoughts?

2
  • You can obviously parse short date format and create your own based on order of d-m-y there... But it may be better and definitely simple to just use an existing format... 5/6/2007 is painful enough to figure out if you happen to be accustomed to other order, I see no value adding year to pain 5/6/7 is perfectly valid date sometime ago... but which one of 27 is hard to know :) Commented Jun 19, 2013 at 19:50
  • Not sure what you are getting at here. I think you just reiterated my question. I want to make it better and simpler by tailoring the date format to my user's DateTimeFormat. Commented Jun 19, 2013 at 19:59

2 Answers 2

1

You could retrieve the current ShortDate format from current culture, change it and use it with ToString()

var currentDate = DateTime.Now;
var shortDateFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
var newShortDateFormat = shortDateFormat.Replace("yyyy", "yy");
Console.WriteLine(currentDate.ToString(shortDateFormat));
Console.WriteLine(currentDate.ToString(newShortDateFormat));
Sign up to request clarification or add additional context in comments.

3 Comments

This would work if you created a new CultureInfo with the altered ShortDatePattern. So instead of passing a string to ToString(), you would pass a CultureInfo with its ShortDatePatter modified.
@user912447 That is also a way to realize it.
Huh, not sure why I thought that wouldn't work at first. I just did it the way I suggested and it is far messier than it has to be.
0

System.Globalization.CultureInfo implements IFormatProvider, so you can provide a CultureInfo object as a parameter to the ToString method.

MSDN seems to have an example of exactly what you want.

9 Comments

Unless I am mistaken, I don't see a way to customize that. That will just print a generic date in whatever culture you give it. As in, the culture would display the Date aspect as "D/M/YYYY". I still need to give it a custom format to get "D/M/YY"
Update: Yeah I tested it and if you give ToString() a custom string format then that custom string format overrides the CultureInfo.
Sorry, you're absolutely right. As a temporary solution (although it seems hacky), how about taking the ShortDatePattern (msdn.microsoft.com/en-us/library/…) and replacing yyyy with yy?
Interesting. That's certainly a solution. I agree, a little hacky, but that makes it more fun :)
Hhm, the ShortDatePattern is read-only and I don't see a way to set it as otherwise. There is a SetAllDateTimePatterns() function in the DateTimeFormat but I'm not sure if that will do it yet. msdn.microsoft.com/en-us/library/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.