Display date in arabic in this format 'Wednesday, May 22, 2013' - c#

I have a multilingual website and I need to show the date in this format Wednesday, May 22, 2013. I use following line of code to display dates
DateTime.UtcNow.ToString("D");
That doesn't work when I use this along with Culture info
It then shows me dates in this format مايو 2013,22
How can I show date in arabic in format year, date month day
I am not sure why DateTime.UtcNow.ToString("D"); is not able to convert to arabic date with weekdays
UPDATE : this works DateTime.Now.ToString("dd dddd , MMMM, yyyy", new CultureInfo("ar-AE"))

You can specify the format explicitly:
DateTime.UtcNow.ToString("dddd, MMMM dd, yyyy");
This will output, for example:
Wednesday, May 22, 2013
A lowercase 'd' stands for the day, two 'd's for the day with a zero in front if applicable, three 'd's for the abbreviation of the name of the day and four 'd's for the complete name of the day. The same applies to the months with a capital 'M' (lowercase 'm' are minutes!).

Use CultureInfo class to convert date formats in different languages.
using System.Globalization;
DateTime.UtcNow.ToString("D", new CultureInfo("ar-AE"));

Related

Datetime format issue: how to convert datetime with 4 digit days or months

I'm trying to parse DateTime data from podcast XML.
Basically, it comprises http header format.
(Format-A) Fri, 28 Aug 2015 00:00:00 EST
But, sometimes it has the different format with 4 digit days and month like below.
(Format-B) Thur, 30 July 2015 00:00:00 EST
I don't know why the Podcast provides 2 different formats at the same time.
I thought I could simply parse this format as this website mentioned.
https://www.dotnetperls.com/datetime-parse
But it didn't work with just DateTime.Parse() method
So I wrote this code.
try
{
dtPubDate = DateTime.ParseExact(strhttpTime,
"ddd, dd MMM yyyy HH:mm:ss 'EST'",
CultureInfo.InvariantCulture.DateTimeFormat,
DateTimeStyles.AssumeUniversal);
}
catch (FormatException)
{
dtPubDate = DateTime.ParseExact(strhttpTime,
"dddd, dd MMMM yyyy HH:mm:ss 'EST'",
CultureInfo.InvariantCulture.DateTimeFormat,
DateTimeStyles.AssumeUniversal);
}
As I wrote this code, if the first format doesn't work, try the second one.
But it still got the exception with Format-B.
This URL is what am having the problem with.
http://www.thebreathingmusic.com/podcast/podcast.xml
As you can see, there are 2 different formats with pubDate tag.
What am I missing?
The format string dddd will match against the entire day name, e.g. "Thursday", not 4 characters.
From the docs:
The "dddd" custom format specifier (plus any number of additional "d" specifiers) represents the full name of the day of the week. The localized name of the day of the week is retrieved from the DateTimeFormatInfo.DayNames property of the current or specified culture.
You could just trim everything before the comma and parse that instead.
var tidyDate = strhttpTime.Split(',')[1].Trim();
dtPubDate = DateTime.ParseExact(
tidyDate,
"ddd, dd MMM yyyy HH:mm:ss 'EST'",
CultureInfo.InvariantCulture.DateTimeFormat,
DateTimeStyles.AssumeUniversal);

Custom Long Date Format - C#

I have a drop down menu containing several different dates, where one will be selected based in if it matches a date in an Excel sheet I'm using. The dates in the dropdown menu are of the format Wednesday, August 9, 2016.
Now my plan was to convert the Excel data to a long date format and compare the two; however, I noticed that C#'s long date format includes a zero preceding the day, like Wednesday, August 09, 2016.
Is there a way that I can remove that extra 0 in front of the 9 when converting to a long date format in C#? Or is there another method I should try.
This should do:
mydate.ToString("dddd, MMMM d, yyyy", CultureInfo.CreateSpecificCulture("en-US"));
If your format is fixed that way, you can just use a custom format string:
date.ToString("dddd, MMMM d, yyyy", CultureInfo.GetCultureInfo("en-us"));
this format can do the job:
String date = DateTime.Now.ToString("dddd,MMMM,d,yyyy",CultureInfo.GetCultureInfo("en-us"));
// Wednesday, August 9, 2016

Hyphens at DateTime.Parse?

var d=DateTime.Parse("03-02-2013");
Console.Write(d.Month); //2
1) How does Dateime.parse knows that :
- is the separator ? the - is not a standard separator (http://msdn.microsoft.com/en-us/library/az4se3k1.aspx)
2)How does it knows that the month is 2 and not 3 ? is it by the regional settings ? ( I changed my regional settings and it wasn't changed)... I try to find a reference with MSDN but couldn't find any.
This is my DateTimeFormatInfo.CurrentInfo data :
DateTime.Parse as opposed to DateTime.ParseExact tries to do a best effort to parse your date. This means that it allows a number of different date separators including /, -, . and (space). However, the sequence of the components of the date is still inferred from DateTimeFormatInfo.CurrentInfo which in your case dd/MM/yyyy meaning that the day comes before the month.
So with your Hebrew culture DateTime.Parse("03-02-2013") return February 3 2013 while calling DateTime.Parse("03-02-2013", CultureInfo.InvariantCulture) would return March 2 2013 because the invariant culture is based on the en-US culture which has the month before the day.
Exactly how DateTime.Parse behaves is a bit hard to figure out so the following information may not be entirely accurate. I believe that DateTime.Parse will try to look for various formats which may include D, d, y, T, t and also the pattern defined by DateTimeFormat.MonthDayPattern. Given a DateTimeFormatInfo you can get all the patterns using this code:
new[] { 'D', 'd', 'y', 'T', 't' }
.SelectMany(p => dateTimeFormatInfo.GetAllDateTimePatterns(p))
.Concat(new[] { dateTimeFormatInfo.MonthDayPattern })
For the Hebrew culture I get the following list:
dddd dd MMMM yyyy
dd MMMM yyyy
dddd dd 'ב'MMMM yyyy
ddd dd 'ב'MMMM yyyy
dd 'ב'MMMM yyyy
dd/MM/yyyy
dd MMMM yyyy
dd/MM/yy
dd/MMMM/yyyy
dd-MM-yy
dd-MM-yyyy
dd-MMMM-yyyy
yyyy-MM-dd
dd 'ב'MMMM yyyy
dd MMM yy
MMMM yyyy
HH:mm:ss
hh:mm:ss tt
HH:mm
hh:mm tt
dd MMMM
This list includes dd-MM-yyyy, but again, this list may not be entirely accurate.
DateTime.Parse based on the current culture. Here how it is impelement in .NET;
public static DateTime Parse(string s)
{
return DateTimeParse.Parse(s, DateTimeFormatInfo.CurrentInfo, DateTimeStyles.None);
}
I think your current culture date seperetor is - that's why it is working.
From DateTimeFormatInfo.CurrentInfo
Gets a read-only DateTimeFormatInfo object that formats values based
on the current culture.
EDIT: Okey, I'm going to more deep in this subject. As I found on internet, DateTime.Parse supports a lot of formats. For example;
Standart Time "1/1/2000"
HTTP Header "Fri, 27 Feb 2009 03:11:21 GMT";
w3.org "2009/02/26 18:37:58";
nytimes "Thursday, February 26, 2009"
Standart Time "February 26, 2009";
ISO Standard 8601 for Dates "2002-02-10";
Windows file system Created/Modified "2/21/2009 10:35 PM";
Windows Date and Time panel "8:04:00 PM";
How does it knows that the month is 2 and not 3 ?
This is all about in your culture. With InvariantCulture it returns 3, in your culture it returns 2 because of standart date time format.

converting a string to date in the exact manner

I have a string "11 Jan 2011" which I want to convert to the datatype date (i.e 11 Jan 2011).
I have tried all resources about datetime.parse, datetime.parse exact but all these things gives me the same output 2011/01/11 12:00:00 AM. I really don't understand this behaviour. I tried the following:
1.DateTime date = DateTime.Parse("11 Jan 2011");
2.DateTime date = DateTime.ParseExact("11 Jan 2011" , #"dd MMM yyyy", System.Globalization.CultureInfo.InvariantCulture);
parsing and displaying are not the same thing
you parse the original string to a DateTime object but display results using Date/Time format strings
Both your calls are correct.
A DateTime structure preserves no information about formatting; it just represents the raw date and time.
What you need to do is ensure that when you display your date, you do so in the correct format - e.g. by calling string displayString = date.ToString("dd MMM yyyy");

Why is not the day of the week formated in long date format for all languages?

We have used date.ToString ("D") for formatting the date. But when we globalizat the application to other languages ​​we encounter problems. We expect the day of the week to appear when we use the long date format, but it's not lake so for all languages. According http://www.basicdatepicker.com/samples/cultureinfo.aspx, it is far from all language that print out day a week for the long date format. How should we do to format the date?
If you need an explicit format, specify it like so;
DateTime date = DateTime.Now;
string s = date.ToString("dddd, dd MMMM yyyy");
According to the MSDN doco, the "D" format specifier is affected by the Calendar, which is Culture specific.
You shouldn't make assumptions about string formatting or Date display in particular, when internationalising.

Categories

Resources