I have a date format string "dd/mm/yyyy hh:mm". This is working fine for reading and displaying dates in a localised system. E.g. a DateTime could be displayed as "01/01/2011 12:50" or "01.01.2011 12:50" depending on the users Culture settings.
Now I want to display a message to the user that says "Enter the date in this format dd/mm/yyyy hh:mm" but obviously this message is going to have to change depening on the culture too. Is there an easy way of changing the string "dd/mm/yyyy hh:mm" to "dd.mm.yyyy
hh:mm" or whatever, depending on the CurrentCulture?
Yea, that was the other solution I came up with as well. My only worry would be that ShortDatePattern and ShortTimePattern combined wouldn't always match 'dd/mm/yyyy hh:mm'. Is it possible for example that the day and month in ShortDatePattern might be switched
around to mm/dd/yyyy as in the American style?
Well the format string used to display and parse the date is specifically "dd/mm/yyyy hh:mm". I'd use one of the predefiend formats but there doesn't seem to be any predefined format that includes date and time but excludes seconds.
WaterWolf123...
Member
15 Points
31 Posts
How to display a localised date format message to a user.
Jul 09, 2012 01:20 PM|LINK
I have a date format string "dd/mm/yyyy hh:mm". This is working fine for reading and displaying dates in a localised system. E.g. a DateTime could be displayed as "01/01/2011 12:50" or "01.01.2011 12:50" depending on the users Culture settings.
Now I want to display a message to the user that says "Enter the date in this format dd/mm/yyyy hh:mm" but obviously this message is going to have to change depening on the culture too. Is there an easy way of changing the string "dd/mm/yyyy hh:mm" to "dd.mm.yyyy hh:mm" or whatever, depending on the CurrentCulture?
I could use
string messageDate = "dd/mm/yyyy hh:mm"; messageDate = messageDate.Replace("/", DateTimeFormatInfo.CurrentInfo.DateSeparator); messageDate = messageDate.Replace(":", DateTimeFormatInfo.CurrentInfo.TimeSeparator);but is there a better way of doing this?
michaelalex7
Member
170 Points
35 Posts
Re: How to display a localised date format message to a user.
Jul 09, 2012 01:41 PM|LINK
Hi,
You could try the following:
string messageDate = string.Format("{0} {1}", System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern,
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern);
Hope this helps,
WaterWolf123...
Member
15 Points
31 Posts
Re: How to display a localised date format message to a user.
Jul 10, 2012 10:38 AM|LINK
Yea, that was the other solution I came up with as well. My only worry would be that ShortDatePattern and ShortTimePattern combined wouldn't always match 'dd/mm/yyyy hh:mm'. Is it possible for example that the day and month in ShortDatePattern might be switched around to mm/dd/yyyy as in the American style?
michaelalex7
Member
170 Points
35 Posts
Re: How to display a localised date format message to a user.
Jul 10, 2012 11:11 AM|LINK
That would be a possibility yes. Do you want it so that the format is strictly day, month followed by year?
WaterWolf123...
Member
15 Points
31 Posts
Re: How to display a localised date format message to a user.
Jul 10, 2012 11:33 AM|LINK
Well the format string used to display and parse the date is specifically "dd/mm/yyyy hh:mm". I'd use one of the predefiend formats but there doesn't seem to be any predefined format that includes date and time but excludes seconds.
michaelalex7
Member
170 Points
35 Posts
Re: How to display a localised date format message to a user.
Jul 10, 2012 12:39 PM|LINK
I've just taken a look at the msdn website and this may do the trick:
System.DateTime.Now.ToString("g")
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx
swaiss
Member
433 Points
205 Posts
Re: How to display a localised date format message to a user.
Jul 10, 2012 01:04 PM|LINK
- Add "App_GlobalResources" folder
- Add Resource File.resx
- Add the name and its value in Resource File.resx
* Name : dateFormat , Value : dd/mm/yyyy hh:mm
- copy the file twice, now u have 3 files with the same name but different extension like this:
* ValidationResources.resx
* ValidationResources.en.resx
* ValidationResources.gr.resx
- change just the values in the other resouces file
in code behind use this code to get the message
hope this help you.