I'm trying to use DateTime.TryParseExact as below:
DateTime modifiedSinceDateTime;
var succeeded = DateTime.TryParseExact(modifiedSince, "yyyy-MM-ddThh:mm:ss",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out modifiedSinceDateTime);
but it fails with this DateTime value: 2013-06-06T22:41:20 which suggests my date time pattern is not right. I think the pattern doesn't support 24 hours timing format, only up to 12 hours
What should be the correct date pattern like?
Simple enough - change the hh to HH.
hh is for 12 hour clocks, HH for 24 hours, as can be seen in the documentation for Custom Date and Time Format Strings.
Related
I am trying to set the DateTime format to 24 hours. Originally I have a string with a 12 hour representation. All solutions I have found are converting DateTime to string.
string dateString = "Mon 16 Jun 8:30 AM 2008"; // <-- Valid
string format = "dd/MM/yyyy HH:mm";
DateTime dateTime;
if (DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind, out dateTime))
{
DateTime dateIn24 = dateTime;// dateIn24 should be in 24 hour format
}
Is there anything we can do in web.config? Like the following:
<globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8" culture="en-GB" uiCulture="en-GB"/>
i got the answer
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
if its en-US 12 hour format
by default it will be en-US based on system date time settings
The DateTime instance only contains the information, and the Hour property is an integer from 0 to 23, according to MSDN documentation:
Property Value
Type: System.Int32
The hour component, expressed as a value between 0 and 23.
https://msdn.microsoft.com/library/system.datetime.hour(v=vs.110).aspx
If you're talking about formatting it, then you need to convert it to a string, like mentioned in the comments.
As far as I know, the 24-hour format is neither not a matter of how the DateTime is parsed, nor saved, but rather of the formatting. Given that you enter the if-block (I have not been successful on parsing youre dateString in LinqPad) you can obtain a correctly formatted date string with
var dateStringWith24Hours = dateTime.ToString(dateString);
since the HH in your format string means that you'd like to format the hours as 24 hours.
I have an MVC application.
At controller(from view) I am getting start date as string "Tue Jan 01 2008 00:00:00 GMT 0100 (Central Europe Standard Time)".
Could anybody please tell me how to convert this datetime to normal dd-mmy-yyyy hh:mm:ss at Controller level.
Thanks in advance....
You need to use the DateTime.ParseExact method and supply the correct format string so the code knows what each part of the input string represents.
CultureInfo provider = CultureInfo.InvariantCulture;
string format = "ddd MMM dd yyyy hh:mm tt zzz";
result = DateTime.ParseExact(dateString, format, provider);
Double check the format strings against the documentation there are several subtle and not so subtle gotchas:
"h" represents the hour in 12 hour clock
"hh" represents the hour in 12 hour clock with leading zero
"HH" represents the hour in 24 hour clock with leading zero
"m" represents a minute from 0 to 59
"M" represents a month from 1 to 12
etc.
So you may find that you think your format string is OK for the examples you have, but then a new string will come along which won't parse. You'll have to examine it to see if you've got hours in 12 or 24 hour format, etc.
Though you will have to strip of the timezone name from the end of the string first as that won't be recognised by ParseExact.
This is a very strange date fromat I have never seen before coming back from some API in JSON.
"Tue Aug 04 2015 00:17:38 GMT+0000 (UTC)"
That is generating the following error:
System.FormatException: String was not recognized as a valid DateTime.
Which is understandable when using the following method to parse:
DateTime.Parse(x.process_date.Value)
Anyone dealt with complex date formats that may know how to parse that?
You can use the DateTime.ParseExact method (or DateTime.TryParseExact, to cleanly handle parsing failures) to accomplish this. These methods allow you to explicitly specify the format string.
Something like this could work:
var dateString = "Tue Aug 04 2015 00:17:38 GMT+0000 (UTC)";
var format = "ddd MMM dd yyyy HH:mm:ss GMT+0000 (UTC)";
var parsed = DateTime.ParseExact(
dateString,
format,
System.Globalization.CultureInfo.InvariantCulture);
Or, using TryParseExact:
DateTime parsed;
if (DateTime.TryParseExact(
dateString,
format,
System.Globalization.CultureInfo.InvariantCulture,
DateTimeStyles.None,
out parsed)
{
// parsing was successful
}
else
{
// parsing failed
}
Here's a breakdown of the format string used here:
ddd - The abbreviated name of the day of the week.
MMM - The abbreviated name of the month.
dd - The day of the month, from 01 through 31.
yyyy - The year as a four-digit number.
HH:mm:ss - The hour, using a 24-hour clock from 00 to 23; the minute, from 00 through 59; and the second, from 0 through 59 (delimited by : characters).
GMT+0000 (UTC) - just static text that the format string assumes will always be present. This is pretty brittle and could cause your parsing to fail if the API ever returns different text here. Consider truncating this text, or using NodaTime which offers great support for time zones.
You might need to tweak this format string slightly to your usage -- for example, it wasn't clear from your question whether or not you are using a 12-hour clock or a 24-hour clock.
For more information on how to build a format string, see Custom Date and Time Format Strings on MSDN.
Alternatively, you could eschew using System.DateTime in favor of NodaTime. I'm less familiar with NodaTime myself, but great documentation is available both here on StackOverflow and on NodaTime's site.
i have a string which contains date time this...
string S="08/18/2013 24:00:00"
DateTime DT = DateTime.ParseExact(S, "MM/dd/yyyy HH:mm:ss", null);
i want to parse it into date time but shows an exception like this.
The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.
please tell me any solution for this problem.
The problem is with the hour being 24. DateTime doesn't support this, as far as I'm aware.
Options:
Use my Noda Time project which does support 24:00:00, but basically handles it by adding a day (it doesn't preserve a difference between that and "end of previous day")
Keep using DateTime, manually replace "24:00:00" with "00:00:00" when it occurs, and remember to add a day afterwards
If you want to preserve the information that it was actually "end of the day" you'd need to do that separately, and keep the information alongside the DateTime / LocalDateTime.
You should also parse with the invariant culture as other answers have suggested - you're not trying to parse a culture-specific string; you know the exact separators etc.
string S="08/18/2013 00:00:00"; // here is the first problem occurred
DateTime DT = DateTime.ParseExact(S, "MM/dd/yyyy hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
From The "HH" Custom Format Specifier
The "HH" custom format specifier (plus any number of additional "H"
specifiers) represents the hour as a number from 00 through 23; that
is, the hour is represented by a zero-based 24-hour clock that counts
the hours since midnight.
So, using 24 as an hour is invalid on this case.
Try with hh format with 00 instead like;
string S = "08/18/2013 00:00:00";
DateTime DT = DateTime.ParseExact(S, "MM/dd/yyyy hh:mm:ss", CultureInfo.InvariantCulture);
Here a DEMO.
If you really want to use 24:00:00 as a hour, take a look Noda Time which developed by Jon.
I am adding 12 hours to the current time. but the current time is displayed in the text box, what is wrong with the code
DateTime expiresAt = System.DateTime.Now.AddHours(12);
txt_ExpiresBy.Text = expiresAt.ToString(#"dd/MM/yyyy hh:mm:ss");
Maybe you are adding 12 hours and you don't see the difference between X AM and X PM?
Try using HH (hour in 24 hours format) instead of hh (hour in 12 hours format) in the format string, or adding the AM/PM indicator tt:
// 24 hours format
expiresAt.ToString(#"dd/MM/yyyy HH:mm:ss");
// 12 hours + am/pm
expiresAt.ToString(#"dd/MM/yyyy HH:mm:ss tt");
See Custom Date and Time Format Strings for a complete reference.