I have run into an issue. I'm obtaining a date time string from the database and and some of these date time strings does not contain time. But as for the new requirement every date time string should contain the time like so,
1)1980/10/11 12:00:01
2)2010/APRIL/02 17:10:00
3)10/02/10 03:30:34
Date can be in any format followed by the time in 24hr
notation.
I tried to detect the existence of time via the following code,
string timestamp_string = "2013/04/08 17:30"; DateTime timestamp = Convert.ToDateTime(timestamp_string); string time =""; if (timestamp_string.Length > 10) { time = timestamp.ToString("hh:mm"); } else { time = "Time not registered"; } MessageBox.Show(time);
But this only works for the No 1)
type timestamps. May I please know how to achieve this task on how to detect if the time element exist in this date time string. Thank you very much :)
POSSIBLE MATCH How to validate if a "date and time" string only has a time?
INFO the three answers provided by Arun Selva Kumar
,Guru Kara
,Patipol Paripoonnanonda
are all correct and checks for the time and serves my purpose. But I select Guru Kara
s answer solely on ease of use and for the explanation he has given. Thank you very much :) very much appreciated all of you :)
TryParseExact("15:30:20", "yyyy-MM-dd HH:mm:ss",CultureInfo. InvariantCulture,DateTimeStyles. AssumeLocal,out datetime); To check if a string is parsable into DateTime.
The date time components TimeOfDay is what you need.
MSDN says "Unlike the Date property, which returns a DateTime value that represents a date without its time component, the TimeOfDay property returns a TimeSpan value that represents a DateTime value's time component."
Here is an example with consideration of all your scenarios.
Since you are sure of the format you can use DateTime.Parse
else please use DateTime.TryParse
var dateTime1 = System.DateTime.Parse("1980/10/11 12:00:00"); var dateTime2 = System.DateTime.Parse("2010/APRIL/02 17:10:00"); var dateTime3 = System.DateTime.Parse("10/02/10 03:30:34"); var dateTime4 = System.DateTime.Parse("02/20/10"); if (dateTime1.TimeOfDay.TotalSeconds == 0) { Console.WriteLine("1980/10/11 12:00:00 - does not have Time"); } else { Console.WriteLine("1980/10/11 12:00:00 - has Time"); } if (dateTime2.TimeOfDay.TotalSeconds == 0) { Console.WriteLine("2010/APRIL/02 17:10:00 - does not have Time"); } else { Console.WriteLine("2010/APRIL/02 17:10:00 - Has Time"); } if (dateTime3.TimeOfDay.TotalSeconds == 0) { Console.WriteLine("10/02/10 03:30:34 - does not have Time"); } else { Console.WriteLine("10/02/10 03:30:34 - Has Time"); } if (dateTime4.TimeOfDay.TotalSeconds == 0) { Console.WriteLine("02/20/10 - does not have Time"); } else { Console.WriteLine("02/20/10 - Has Time"); }
Try this,
DateTime myDate; if (DateTime.TryParseExact(inputString, "dd-MM-yyyy hh:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out myDate)) { //String has Date and Time } else { //String has only Date Portion }
You can try using other format specifiers as listed here, http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With