I have some issue in converting string to date using ParseExact
string _date = " 2014-06-23-12:40:18 "
DateTime dateresult = DateTime.ParseExact(
_date.Trim(),
"yyyy-MM-dd-hh:mm:ss",
CultureInfo.InvariantCulture,
DateTimeStyles.None)
The dateresult's value is 23/06/2014 00:40:18. The problem is that the resulting datetime's hour is 0 not 12!!! I need to keep the normal hour counter from 0 to 23
How can i change my code to fix this error?
Try using HH format specifier instead of hh
DateTime dateresult = DateTime.ParseExact(_date.Trim(), "yyyy-MM-dd-HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None)
Try changing the code like this. Instead of hh, use HH for 24 hour format
string _date = " 2014-06-23-12:40:18 ";
DateTime dateresult = DateTime.ParseExact(_date.Trim(), "yyyy-MM-dd-HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None);
hh format specifier is for 12-hour clock which is 01 to 12.
Use H format specifier which is for 24-hour clock which is 0 to 23.
The "H" custom format specifier represents the hour as a number from 0
through 23; that is, the hour is represented by a zero-based 24-hour
clock that counts the hours since midnight. A single-digit hour is
formatted without a leading zero.
string _date = "2014-06-23-12:40:18";
DateTime dateresult = DateTime.ParseExact(_date.Trim(), "yyyy-MM-dd-H:mm:ss",
CultureInfo.InvariantCulture, DateTimeStyles.None);
Related
I am trying to parse two string values into DateTime.
DateTime processStartTime = DateTime.ParseExact(currentDateTime.Date.ToString("dd-MM-yyyy") + " " + "00:00", "dd-MM-yyyy hh:mm", System.Threading.Thread.CurrentThread.CurrentCulture);
DateTime processEndTime = DateTime.ParseExact(currentDateTime.Date.ToString("dd-MM-yyyy") + " " + "13:00", "dd-MM-yyyy hh:mm", System.Threading.Thread.CurrentThread.CurrentCulture);
The first statement works fine, but the second statement fails with exception-
String was not recognized as a valid DateTime
What I am doing wrong?
You have to use HH:mm instead of hh:mm for 24h format
The "hh" custom format specifier:
represents the hour as a number from 01 through 12; that is, the hour
is represented by a 12-hour clock that counts the whole hours since
midnight or noon.
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. A single-digit hour is formatted with a
leading zero.
Are you really converting a DateTime object to a string and then convert it back to a DateTime? Otherwise you could just write:
var startTime = currentDateTime.Date;
var endTime = currentDateTime.Date.AddHours(13);
Following code returns false for me and I just can't figure out what am I doing wrong.
var localDateTimeString = "03/24/2016 21:05"; // subject.Substring(0, 16);
DateTime localDateTime;
if (!DateTime.TryParseExact(
localDateTimeString,
"MM/dd/yyyy hh:mm",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out localDateTime)) return false;
Format is fixed 16 char string, always 2 digits for days, month, minutes and hour. 4 digits for year.
But this code returns false, how can I fix this?
Short version it should be HH not hh. Unless the single digit hours (0 to 9) are returned as a single digit; in that case you should use H.
From MSDN:
HH: The hour, using a 24-hour clock from 00 to 23.
hh: The hour, using a 12-hour clock from 01 to 12.
H: The hour, using a 24-hour clock from 0 to 23.
h: The hour, using a 12-hour clock from 1 to 12.
Essentially change your format from:
"MM/dd/yyyy hh:mm"
To:
"MM/dd/yyyy HH:mm"
The correct format should be MM/dd/yyyy HH:mm
if (!DateTime.TryParseExact(
localDateTimeString,
"MM/dd/yyyy HH:mm",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out localDateTime))
I'm trying to remove hidden characters from a string that represents a date time. I'm using .Net Fiddle and you can see the line that tries to ParseExact fails.
Here is a snippet. Please refer to the fiddle link for working code.
var dateTime = "2015-04-14 07:30:00 PM"; //<= this throws an error from some hidden char
dateTime = Regex.Replace(dateTime, #"[^\w:\s-]", "");
Console.WriteLine(dateTime);
DateTime dateWithTime = DateTime.ParseExact(dateTime, "yyyy-MM-dd HH:mm:ss tt",
CultureInfo.InvariantCulture);
Console.WriteLine("OK");
The HH in the format string refers to the 24-hour clock hours, which doesn't work when using AM/PM in the format string for PM times.
Change HH to hh.
It's not an invisible character. Your use of HH conflicts with your use of tt. HH is 24 hour time, but you are using tt to interpret PM (12 hour time). Change it to hh and it works.
var dateTime = "2015-04-14 07:30:00 PM";
//dateTime = Regex.Replace(dateTime, #"[^\w:\s-]", ""); <= not needed
Console.WriteLine(dateTime);
DateTime dateWithTime = DateTime.ParseExact(dateTime, "yyyy-MM-dd hh:mm:ss tt",
CultureInfo.InvariantCulture);
Console.WriteLine("OK");
You need to change HH to hh.
yyyy-MM-dd hh:mm:ss tt
I am trying to parse the following 09/04/2015 17:22:29.183 PM
The code is as follows:
string s = "09/04/2015 17:22:29.183 PM";
DateTime.ParseExact(s,Constants.DateTimeFormat,System.Globalization.CultureInfo.InvariantCulture);
The DateTimeFormat is of the form dd/MM/yyyy hh:mm:ss.fff tt
However the compiler throws up the error. Where am I going wrong?
hh specifier is for 12-hour clock format which takes 01 to 12, you need to use HH specifier which is for 24-hour clock format which takes 00 to 23.
string s = "09/04/2015 17:22:29.183 PM";
DateTime dt;
if(DateTime.TryParseExact(s, "dd/MM/yyyy HH:mm:ss.fff tt", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
dt.Dump(); // 09.04.2015 17:22:29
}
But on the other hand, AM or PM designators are for 12-hour clock format. That's why there is no such a thing as 17 PM or 17 AM.
You need to use HH instead of hh.
Of course, it's a bit weird to use HH and tt at the same time. Are you sure that's what you want to do?
if i have this in a string : 10/13/2010 8:38:40 AM
how to make it to this: 13/10/2010 08:38:40
thank's in advance
DateTime.ParseExact("10/13/2010 8:38:40 AM","MM/dd/yyyy h:mm:ss tt",CultureInfo.InvariantCulture).ToString("dd/MM/yyyy HH:mm:ss")
edited to make sure 24 hour clock is used in output
Use DateTime.Parse() to convert to a true DateTime object and then use the DateTime.ToString() method to output to the format you desire (code example coming):
var dateTime = DateTime.Parse("10/13/2010 8:38:40 AM");
var formattedString = dateTime.ToString("dd/MM/yyyy HH:mm:ss);
Quick and dirty:
DateTime.Parse("10/13/2010 8:38:40 AM", new CultureInfo("en-US")).ToString(new CultureInfo("en-GB"));
Since I know that those formats are for those cultures. However, you can read more about datetime formatting at:
http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx
Standard formatting:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx
Custom formatting:
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
Or for a more general solution, just pass a format string to DateTime.ToString('formatString'). For example, what you want is DateTime.ToString("dd/MM/yyyy HH:mm:ss"). This allows you to make any format you want.
Example:
DateTime exDT = DateTime.Now;
string exOut = exDT.toString("dd/MM/yyyy HH:mm:ss");
Here's a cheat sheet! You can use ":" where you want it
d Short Date
D Long Date
t Short Time
T Long Time
f Full date and time
F Full date and time (long)
g Default date and time
G Default date and time (long)
M Day / Month
r RFC1123 date
s Sortable date/time
u Universal time, local timezone
Y Month / Year
dd Day
ddd Short Day Name
dddd Full Day Name
hh 2 digit hour
HH 2 digit hour (24 hour)
mm 2 digit minute
MM Month
MMM Short Month name
MMMM Month name
ss seconds
tt AM/PM
yy 2 digit year
yyyy 4 digit year
var strfrom = "10/13/2010 8:38:40 AM";
DateTime dt = DateTime.Parse(strfrom, new CultureInfo("en-US"));
Console.WriteLine(dt.ToString(new CultureInfo("en-GB")));
var curDate = DateTime.Now.ToString() ;
string customDateFormat = Convert.ToDateTime(curDate).ToString("dd/MM/yyyy");
another variant, one one line:
DateTime.Parse("10/13/2010 8:38:40 AM", new CultureInfo("en-US")).ToString("dd/MM/yyyy HH:mm:ss");