简体   繁体   中英

String to Time format only ( No Date)

I have a string whose value is 16:00:00 i want to convert it to time format hh:mm:ss.. i dont want date

TimeSpan.ParseExact(splitLine[6], "hh:mm:ss", null); is throwing error that system.time cannot be coverted to system.date time

DateTime.ParseExact(splitLine[6], "hh:mm:ss", CultureInfo.InvariantCulture); is returning date time with adding Today's date automstically.

I just want time format.

please help me.

From MSDN :

The ParseExact method uses the conventions of the culture specified by the formatProvider parameter only if format is a standard TimeSpan format string whose value is either "g" or "G". The "c", "t", and "T" standard format strings use the formatting conventions of the invariant culture. Custom format strings define the precise format of the input string and use literal characters to separate the components of a time interval.

then, you can use this to achieve your goal.

TimeSpan.ParseExact(splitLine[6], "T", CultureInfo.InvariantCulture);

What about below code. It's working fine for me.

string time = "16:00:00";
TimeSpan.Parse(time);

With Custom TimeSpan Format Strings you have to escape delimiters such as colons, for example:

TimeSpan.ParseExact(splitLine[6], "hh':'mm':'ss", null)

or:

TimeSpan.ParseExact(splitLine[6], @"hh\:mm\:ss", null)

Note that this is different from DateTime format strings where some delimiters, like colon : , have a special meaning while others, like period . , have no meaning but don't need to be escaped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM