And Idea Why the Year appears as 2555?
The culture of the site is Thai
Yes - any time DateTime is converted to a string with no explicit culture specified, it will use the current culture's calendar system. However, the DateTime components themselves still reflect the Gregorian calendar.
You'll see the 2555 if you use:
int thaiYear = new ThaiBuddhistCalendar().GetYear(DateTime.Now);
Basically, if you want to get culture-specific date information programmatically, you need to use a System.Globalization.Calendar. When formatting a date, make sure you specify the right culture for the calendar you want to use.
The Thai have a different calendar.
Let me quote:
There is a 543 years difference between the Buddhist calendar and the
Gregorian calendar. Year 2012 in Europe is year 2555 in Thailand.
From http://www.thaiworldview.com/feast/feast.htm
Related
I have a textbox with this mask: year/ month /day hour :min
The datetime format is Persian like 1392/12/11 12:43
I need to convert this string to standard English format so I used pesiancalender class.
As you can see the function todate() expects the values separately, I don't know how can I separate the string to this values! I mean I don't know how can I detect year and month and day and hour and min in string.
You can use either DateTime.TryParseExact method with providing the date format to it with culture info about persian date.
Edit: as I found out:
Currently, the PersianCalendar class is not an optional calendar for any culture supported by the CultureInfo class and consequently cannot be a default calendar.
So, you can't use the approach I've suggested. Some investigation led me to the this project for working with Persian date time and some hacks for the CultureInfo.
Such questions were already on SO, so I suggest to use their approach, and to write some helper class to solve your problem.
How can I format the date and time depending on the users region settings in an ASP.NET-MVC application without worrying about the order of the date?
For example, I want to have:
the day with a leading zero (dd);
month abbreviated three-letter form (MMM);
full year (yyyy);
the time just the hours and minutes both with leading zeros (HH:mm);
depending on if the user is from USA show AM/PM after the time;
Every country displays the date in a different order. USA displays first the month than days than years (MMM/dd/yyyy). In China first the year than month than day (yyyy-MMM-dd) (IIRC). And in Europe most countries display the date in this format: dd-MMM-yyyy.
And then not to mention the slashes/dashes used to separate the month, days and years from each other in every country.
This of course can be done with an endless if/else or switch statement, but isn't there a more elegant way to do this?
EDIT this is the best I came up with:
var cltr = System.Threading.Thread.CurrentThread.CurrentCulture;
DateTime.Now.ToString(cltr.DateTimeFormat.ShortDatePattern + " " + cltr.DateTimeFormat.ShortTimePattern)
It displays the date in numbers only. How would I change that in short month notation but not changing the order and the separators etc?
You can get the culture currently used by the user with CurrentCulture and CurrentCultureInfo.
For more details, see: http://msdn.microsoft.com/en-us/library/bz9tc508.aspx
I think you are talking about formatting issue. Once you have your DateTime object, then you can display it as you want. You have to specify new CultureInfo("en-US") as IFormatProvider in the DateTime.ToString() method if you want to show the datetime as US format.
Why should we use IFormatProvider in DateTime.ParseExact if there is already a format parameter?
DateTime.ParseExact(inputString, format, cultureInfo);
The format parameter says what pattern to use - but it doesn't say anything about which calendar, month names, short date format etc to use. That's up to the IFormatProvider.
For example, suppose you wanted to parse a value with the pattern "dd MMMM yyyy" - which month names would you expect to work? If you're using a month name of "February" but you're running on a machine with a system culture of French, it would fail - you'd need to specify an English culture (or the invariant culture) to get it to work. Likewise, you could specify a pattern of "d" to mean the short date format - but which short date format?
Even the calendar you use is affected by the format provider: the value could be parsed into the same year, month and day values in two cultures - but the meaning of those values would be very different in a Hijri calendar from a Gregorian calendar, for example.
A simple example: /
/ is not just a char, but a date separator that depends on the culture.
I'm having trouble representing Persian (Solar Hijri Calendar) dates as DateTime in C#, specifically on certain days of particular months, for example 31/04 where in the Gregorian calendar such a date is meaningless:
System.Globalization.PersianCalendar p = new System.Globalization.PersianCalendar();
DateTime date = new DateTime(2013,7,22);
int year = p.GetYear(date);
int month = p.GetMonth(date);
int day = p.GetDayOfMonth(date);
DateTime d1 = new DateTime(year, month, day);
The above code will result in an ArgumentOutOfRangeException saying:
Year, Month, and Day parameters describe an un-representable DateTime.
Which is expected.
How can I represent Persian Dates as DateTimes in .NET taking into accounts dates such as 30/02 and 31/04?
The above code will result in an ArgumentOutOfRangeException saying: Year, Month, and Day parameters describe an un-representable DateTime.
Yes, because unless you specify a calendar, the DateTime arguments are expected to be Gregorian. You can specify a calendar though:
DateTime d1 = new DateTime(year, month, day, p);
Note that if you now take d1.Year, you'll get back 2013, not year... DateTime is always Gregorian, basically. However, if you use a culture which has the Persian calendar as the default calendar, that will convert the values appropriately when you format a DateTime into a string. EDIT: Unfortunately, as per the documentation:
Currently, the PersianCalendar class is not an optional calendar for any culture supported by the CultureInfo class and consequently cannot be a default calendar.
As a comment has mentioned Noda Time, I can address that: it doesn't support the Persian calendar yet. It supports the lunar Hijri calendar, but not the solar one :( I could look into adding that into a future release...
From MSDN;
Each DateTime member implicitly uses the Gregorian calendar to perform
its operation, with the exception of constructors that specify a
calendar, and methods with a parameter derived from IFormatProvider,
such as System.Globalization.DateTimeFormatInfo, that implicitly
specifies a calendar.
Also from PersianCalendar Class
Currently, the PersianCalendar class is not an optional calendar for
any culture supported by the CultureInfo class and consequently cannot
be a default calendar.
What you want seems not possible to me.
I have a bit of code that display the date within a text field as shown below
textField.Text = DateTime.Now.ToShortDateString();
It shows as
11/03/2011
anyone know how I could format here to show it as
11/03/11
Thanks in advance
Yes. Take a look at this Date and Time formatting page.
Or: theDate.ToString("dd/MM/yy")
Very simple:
string strFormat = "dd/MM/yy";
textField.Text = DateTime.Now.ToString(strFormat);
note that the format string is case-sensitive, make sure you use capital 'M's for month, otherwise it will consider 'minutes' for 'm'.
More general help about datetime formatting:
MMM: display three-letter month
MM: display two-digit month
ddd: display three-letter day of the WEEK
d: display day of the MONTH
HH: display two-digit hours on 24-hour scale
mm: display two-digit minutes
yyyy: display four-digit year
DateTime.Now.ToString("dd/MM/yy")
DateTime.Now.ToString("dd/MM/yy")
But you need to remember that ToShortDateString() is culture sensitive, returning different strings depending on the regional settings of the computer - the above is not.
You could change the settings on your computer, in Windows 7, you will find the Short Date format under Region and Language in the control panel.
Here is an alternative if you don't like format strings.
var fp = new System.Globalization.CultureInfo("en-GB");
textField.Text = DateTime.Now.ToString(fp.DateTimeFormat.ShortDatePattern);