Answered by:
checking of correct datetime format is not working

Question
-
User-1355965324 posted
Hi
I want to check the given string value is correct format for converting into datetime variable before to convert into date time variable. But the value '05/03/2020 8:15:00' is showed as incorrect format . Please can you advise me where is the problem
eg: datetimestring ='05/03/2020 8:15:00' string clockeddate = log.AttendanceDate; string timeclock = log.Hrs + ':' + log.Mins; string datetimestring = clockeddate + " " + timeclock+":00"; DateTime dDate; bool isCorrectDateFormat = DateTime.TryParseExact(datetimestring, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dDate); if(isCorrectDateFormat) { dDate = Convert.ToDateTime(clockeddate + " " + timeclock); }
Thursday, March 5, 2020 10:19 AM
Answers
-
User753101303 posted
Hi,
HH requires two digits. Try H instead. I tend to use TryParse especially with the invariant culture.
Edit: for example
using System; using System.Globalization; public class Program { public static void Main() {
DateTime dDate; foreach(string value in new string[] {"05/03/2020 8:15:00", "05/03/2020 08:15:00" }) { Console.WriteLine(DateTime.TryParseExact(value, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dDate)); Console.WriteLine(DateTime.TryParseExact(value, "dd/MM/yyyy H:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dDate)); Console.WriteLine(DateTime.TryParse(value,CultureInfo.InvariantCulture,DateTimeStyles.None, out dDate)); }
} }shows False for the very first result and True otherwise.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, March 5, 2020 10:55 AM -
User-854763662 posted
Hi polachan ,
As PatriceSc said , you make the mistake on the Date Format . 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.
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.
Refer to Custom date and time format strings for more details.
Best Regards,
Sherry
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, March 6, 2020 7:46 AM
All replies
-
User753101303 posted
Hi,
HH requires two digits. Try H instead. I tend to use TryParse especially with the invariant culture.
Edit: for example
using System; using System.Globalization; public class Program { public static void Main() {
DateTime dDate; foreach(string value in new string[] {"05/03/2020 8:15:00", "05/03/2020 08:15:00" }) { Console.WriteLine(DateTime.TryParseExact(value, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dDate)); Console.WriteLine(DateTime.TryParseExact(value, "dd/MM/yyyy H:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dDate)); Console.WriteLine(DateTime.TryParse(value,CultureInfo.InvariantCulture,DateTimeStyles.None, out dDate)); }
} }shows False for the very first result and True otherwise.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, March 5, 2020 10:55 AM -
User-1716253493 posted
Hi Polacan,
I assume that you have AttendanceDate in DateTime type, Hrs, Mins in double or integer.
So, why you make a datetime string to get dDate?
You can calculate the values directly without convert it to a string first.
https://docs.microsoft.com/en-us/dotnet/api/system.datetime.addhours?view=netframework-4.8
Friday, March 6, 2020 2:53 AM -
User-854763662 posted
Hi polachan ,
As PatriceSc said , you make the mistake on the Date Format . 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.
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.
Refer to Custom date and time format strings for more details.
Best Regards,
Sherry
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, March 6, 2020 7:46 AM