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.
Related
This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 1 year ago.
Can anybody help me convert this into date time format?
20210115122710
I need in format like
DateTime date = DateTime.ParseExact("20210115122710", "dd/MM/yyyy hh:mm:ss", null);
You are parsing DateTime with wrong format, use "yyyyMMddHHmmss" instead of "dd/MM/yyyy hh:mm:ss",
DateTime date = DateTime.ParseExact("20210115122710", "yyyyMMddHHmmss", null);
Now whenever you want to print it in given format, then use .ToString() method,
string dateInGivenFormat = date.ToString("dd/MM/yyyy HH:mm:ss");
Note: I used HH for hours instead of hh.
HH : Use of HH converts hours in 24-hours format i.e from 00 to 23.
hh : hh converts hours in 12-hours format i.e from 00 to 12.
If your time is in 12-hour format then use hh otherwise use HH.
Try it online
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.
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.
I am unable to convert strings to date with throwing exceptions of not proper format:
here are the patterens:
DD-MM-YYYY
MM-DD-YYYY
YYYY-DD-MM
YYYY-MM-DD
YYYYDDMM
YYYYMMDD
formats are coming from dropdown list and here is what I tried different method but still its throwing exceptions:
strCurrentFormat = rcboDateFormat.SelectedValue.ToString();
DateTime db = DateTime.ParseExact(strOldDate, "DD-MM-YYYY", CultureInfo.InvariantCulture);
//DateTime loadedDate = DateTime.ParseExact(strOldDate, strCurrentFormat, null);
I have solved this problem before by taking string, split them and move the year and month and day around to proper format but that would take long time, if anyone knows an easier way it would be a tremendous help.
Two things:
First - use the correct format strings. D and Y are not known format specifiers. d and y are.
Second - you can put your formats into a string array and use the ParseExact overload that takes that list of formats.
string[] formats = new string[]
{"dd-MM-yyyy",
"MM-dd-yyyy",
"yyyy-dd-MM",
"yyyy-MM-dd",
"yyyyddMM",
"yyyyMMdd"};
DateTime db = DateTime.ParseExact(strOldDate,
formats,
CultureInfo.InvariantCulture,
DateTimeStyles.None);
The formats will be attempted in order.
Taken from http://www.codeproject.com/Articles/14743/Easy-String-to-DateTime-DateTime-to-String-and-For
d - Numeric day of the month without a leading zero.
dd - Numeric day of the month with a leading zero.
ddd - Abbreviated name of the day of the week.
dddd - Full name of the day of the week.
f,ff,fff,ffff,fffff,ffffff,fffffff -
Fraction of a second. The more Fs the higher the precision.
h - 12 Hour clock, no leading zero.
hh - 12 Hour clock with leading zero.
H - 24 Hour clock, no leading zero.
HH - 24 Hour clock with leading zero.
m - Minutes with no leading zero.
mm - Minutes with leading zero.
M - Numeric month with no leading zero.
MM - Numeric month with a leading zero.
MMM - Abbreviated name of month.
MMMM - Full month name.
s - Seconds with no leading zero.
ss - Seconds with leading zero.
t - AM/PM but only the first letter.
tt - AM/PM ( a.m. / p.m.)
y - Year with out century and leading zero.
yy - Year with out century, with leading zero.
yyyy - Year with century.
zz - Time zone off set with +/-.
Your format string is wrong.
You need lowercase for "d" and "y", so
dd-MM-yyyy
Read all about the format string on MSDN.