Custom Long Date Format - C# - 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

Related

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

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"));

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");

Troubles parsing DateTime from string

I am currently trying to parse a string that is obtained from an xml that is downloaded from the web every few minutes. The string looks like this:
Thu Jul 12 08:39:56 GMT+0100 2012
At first I just did a string.split and took out everything after the time (GMT+0100 2012) and inserted 2012 after the date.
This worked great until the date changed to:
Thu Jul 12 08:39:56 GMT+0000 2012
So I would like to dynamically pasre the GMT+ whatever as they send me that string in c#.
Any advice would be appreciated.
You can use DateTime.ParseExact with a custom date and time format string:
DateTime.ParseExact("Thu Jul 12 08:39:56 GMT+0000 2012",
"ddd MMM dd hh:mm:ss 'GMT'K yyyy",
CultureInfo.InvariantCulture)
This will throw a format exception if the string and format string do not match exactly, so you may want to use DateTime.TryParseExact that will return a false if it fails.
Instead of DateTime you may want to use DateTimeOffset that preserved timezone information , as #Keith commented - this may be important to your application.
Two things you can do: First, you should be able to use a custom format string with a ParseExact method, either from DateTime or DateTimeOffset (I would use DateTimeOffset if the actual time zone of the stamp is important, and not just the equivalent time in UTC or your local time zone).
Have a look: DateTime custom format string
The format string would probably be something like #"ddd MMM dd HH:mm:ss 'GMT'zzzz yyyy".
However, there's one snag; the .NET time zone offset ("zzzz" or simply "K") always includes a colon between the hour and minute when expressed as a string, which your input strings do not have. There is no way I know of to specify that the time zone offset doesn't/shouldn't have this colon, and I'm pretty sure that trying to parse it without a colon would cause an error.
The simplest workaround is to remove that specific colon from the string prior to parsing it. The code for that given your input is simply to remove the last colon character in the string:
var updatedString = inputString.Remove(inputString.LastIndexOf(':'), 1);
Try DateTime.Parse method to parse your date.
This should work:
XmlConvert.ToDateTime(textBox1.Text, "ddd MMM dd HH:mm:ss 'GMT'zzzz 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.

DateTime.Parse American Date Format C#

Probably a simple question -
I'm reading in data from a number of files.
My problem is, that when I'm reading in the date from an american file, I parse it like so:
DateSold = DateTime.Parse(t.Date)
This parses the string t.Date into a date format, however it formats the american date to a european date, e.g.
If the date is in the file as 03/01/2011, it is read as the 3rd of January, 2011, when it should be the 1st of March 2011.
Is there a way of doing this so that it formats to the european date?
var dt = DateTime.ParseExact(t.Date, "MM/dd/yyyy", CultureInfo.InvariantCulture);
The DateTime itself has no formatting, it is only when you convert it to or from a string that the format is relevant.
To view your date with American format, you pass the format to the ToString method
string americanFormat = dt.ToString("MM/dd/yyyy");
If you are parsing the date from a file which is specifically a US formatted file then simply pass the US culture information into the parse function as follows;
var usCulture = "en-US";
var dateValue = DateTime.Parse(dateString, new CultureInfo(usCulture, false));
This way you can simply swap out the culture string per different region required for parsing. Also, you no longer have to research the specific datetime format nuances for each culture as .Net will take care of this for you as designed.
Use DateTime.ParseExact or DateTime.TryParseExact when parsing, and specify a format string when you format with ToString too.
Note that there's no such thing as "an American date" after it's been parsed. The DateTime value has no concept of formatting.
It sounds like you're not actually interested in the Parse part so much as the formatting part, e.g.
string formatted = dt.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
... but I would recommend that you control both the parsing and formatting explicitly.
If you have different file formats, you'll need to give different format strings when you read each file. How you then format the data is a separate decision.
If you know the format ahead of time, you can use DateTime.ParseExact, using the American format as your format string.
string formatteddate=DateTime.Now.ToString("d") // output: 11/8/2012
string formatteddate=DateTime.Now.ToString("D") // output: Monday, November 08, 2012
string formatteddate=DateTime.Now.ToString("f") // output: Monday, November 08, 2012 3:39 PM
string formatteddate=DateTime.Now.ToString("g") // output: Monday, November 08, 2012 3:39:46 PM
string formatteddate=DateTime.Now.ToString("d") // output: 11/8/2012 3:39 PM
More date-time format in asp.net is given here.
http://dateformat.blogspot.in/2012/09/date-time-format-in-c-aspnet.html

Categories

Resources