c# - check if date time string contains time -


i have run issue. i'm obtaining date time string database , and of these date time strings not contain time. new requirement every date time string should contain time so,

1)1980/10/11 12:00:01 2)2010/april/02 17:10:00 3)10/02/10 03:30:34

date can in format followed time in 24hr notation.

i tried detect existence of time via 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 works no 1) type timestamps. may please know how achieve task on how detect if time element exist in date time string. thank :)

possible match how validate if "date , time" string has time?

info 3 answers provided arun selva kumar,guru kara,patipol paripoonnanonda correct , checks time , serves purpose. select guru karas answer solely on ease of use , explanation has given. thank :) appreciated of :)

the date time components timeofday need.

msdn says "unlike date property, returns datetime value represents date without time component, timeofday property returns timespan value represents datetime value's time component."

here example consideration of scenarios.
since sure of format 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 - 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 - 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 - not have time"); } else {     console.writeline("10/02/10 03:30:34 - has time"); }  if (datetime4.timeofday.totalseconds == 0) {     console.writeline("02/20/10 - not have time"); } else {     console.writeline("02/20/10 - has time"); } 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -