I would like to know if there is a way to get the current date in the following format :
YYYY.MM.DD
I know that I can get the current date, and then manipulate it to get the desired format, but I was wondering if there is a pattern solution to specify my own format and so directly getting a string like this :
"2014.06.10"
string dateFormatted = DateTime.Today.ToString("yyyy.MM.dd");
You can use the string.Format() method and reference this link http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
The {#} represents the argument being passed and the {#:yyyy} is a way of formatting to grab just the year in format of yyyy. Same for MM and dd. Worth noting is that mm is minutes and MM is months which may be confusing as day and year are also lower case.
string.Format("{0:yyyy}.{0:MM}.{0:dd}", DateTime.Now);
Related
I am trying to convert string dates like 2020-01-14T17:01:48.757Z and 2020-01-14T17:01:50.760Z in to C# DateTime. Looks like my parsing is failing somewhere.
DateTimeOffset.ParseExact("2020-01-14T17:01:48.757Z", "yyyy-MM-dd'T'HH:mm:sszzz", CultureInfo.InvariantCulture).DateTime;
Whats wrong with above code ? It fails with
String '2020-01-14T17:01:50.760Z' was not recognized as a valid
DateTime.
When I parse same date online https://nsdateformatter.com/ It has no issues.
I even tried using yyyy-MM-dd'T'HH:mm:ssZ but it also gives above error.
Use this date format:
DateTimeOffset.ParseExact("2020-01-14T17:01:48.757Z", "yyyy-MM-ddTHH:mm:ss.fffZ", CultureInfo.InvariantCulture).DateTime;
2020-01-14T17:01:48.757Z
yyyy-MM-ddTHH:mm:ss.fffZ
As You see format corresponds to Your provided date string.
Looks like you forget to use proper format specifier for your milliseconds part and dot (.) between your seconds and milliseconds part.
The "fff" custom format specifier
The "fff" custom format specifier represents the three most
significant digits of the seconds fraction; that is, it represents the
milliseconds in a date and time value.
DateTimeOffset.ParseExact("2020-01-14T17:01:48.757Z",
"yyyy-MM-dd'T'HH:mm:ss.fffZ",
CultureInfo.InvariantCulture)
I have datetime in string format which is like this:
24-ago-19 19:21:14
Or format like this:
Aug-24-19 19:21:14
When I use this on second format:
DateTime.Parse(dateTime);
It parses the datetime correctly- however on the first format:
24-ago-19 20:21:14
I get an error like this:
The string was not recognized as a valid DateTime. There is an unknown
word starting at index 3
How can I handle conversion of any date format - and preferably convert it to "Pacific Standard Time" ?
What am I doing wrong here ? Can someone help me out?
You can't parse both ago and Aug with the same CultureInfo without any string manipulation in your custom string format.
Let's run some code first;
foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
if (culture.DateTimeFormat.MonthNames.Contains("ago") ||
culture.DateTimeFormat.AbbreviatedMonthNames.Contains("ago"))
{
if (culture.DateTimeFormat.MonthNames.Contains("Aug") ||
culture.DateTimeFormat.AbbreviatedMonthNames.Contains("Aug"))
{
Console.WriteLine(culture.Name);
}
}
}
This code does not write any CultureInfo name in my computer, and probably wouldn't print in yours either. That means, there is no culture information that includes both of these month name and abbreviate month name. Even I check the genitive month names (which includes on AbbreviatedMonthGenitiveNames and MonthGenitiveNames properties) but no luck.
If you can parse Aug-24-19 19:21:14 string with DateTime.Parse directly, that means your CurrentCulture has Aug as an abbreviated month name and MMM-dd-yy HH:mm:ss as a standart date and time format.
How can I handle conversion of any date format
You can't. You have to know what kind of format your string exactly to prevent ambiguous situations. For example, let's take 01-02-2019 string. Is it January 2nd or February 1st? Can you even tell this by looking only it's format? Different people on different regions around the world read this format differently.
and preferably convert it to "Pacific Standard Time"
After you parse your string successfully (remember your DateTime.Kind should be Local), you can use TimeZoneInfo.ConvertTimeBySystemTimeZoneId method to convert it like;
var myDate = DateTime.Parse("Aug-24-19 19:21:14 ");
var myPSTdate = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(myDate , "Pacific Standard Time");
What am I doing wrong here?
As far as I can see, you are not doing anything wrong. Your inputs are not reliable to parse with the same culture information. That's it. Some people call this situation as garbage in, garbage out. If you can fix your inputs, your duty should be to fix their format first.
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.
Is DateTime format strictly dependent on the language of the OS being used? Because the following doesn't work:
DateTime date = DateTime.Now;
var usCultureInfo = CultureInfo.CreateSpecificCulture("en-US");
Console.WriteLine(date.ToString("dddd MM-dd-yy"),usCultureInfo);
I'd like the result to print out as Saturday, 06-29-2013 but the day gets printed out in Korean 토요일, 06-29-2013.
You are a victim of Composite Formatting overload for Console.WriteLine where you could pass Format string and a series of object to be inserted in the placeholders of the format string
You need to write in this way
Console.WriteLine(date.ToString("dddd MM-dd-yy",usCultureInfo));
and you get the right day text.
See the specs here DateTime.ToString(format, IFormatProvider)
Or simply you can use
string abc=date.ToString("dddd MM-dd-yy");
Here is my code:
a.dateFrom = DateTime.ParseExact(x, "dd/mm/yyyy", null);
And x has value of: 08/03/2012
However, a.dateFrom has value of 08/01/2012. Why?
You should use MM as format for month
As ionden notes, you should have a format of
"dd/MM/yyyy"
Currently you're parsing the second part as minutes (as that's what mm means).
See the documentation for custom date and time format strings for more information. I'd also strongly encourage you to consider using the invariant culture for parsing - if you're using a custom format string, that usually means you don't want to treat the input in a culture-sensitive fashion at all.