简体   繁体   English

字符串到时间格式(无日期)

[英]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 我有一个string其值为16:00:00我想将其转换为时间格式hh:mm:ss .. 我不想要日期

TimeSpan.ParseExact(splitLine[6], "hh:mm:ss", null); is throwing error that system.time cannot be coverted to system.date time 抛出错误,system.time无法转换为system.date时间

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 : 来自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". 仅当format是标准TimeSpan格式字符串(其值为“g”或“G”)时,ParseExact方法才使用formatProvider参数指定的区域性约定。 The "c", "t", and "T" standard format strings use the formatting conventions of the invariant culture. “c”,“t”和“T”标准格式字符串使用不变文化的格式约定。 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格式字符串,您必须转义冒号(如冒号),例如:

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 . 请注意,这是从不同DateTime格式字符串,其中一些分隔符,如冒号: ,有而其他特殊的含义,就像时期. , have no meaning but don't need to be escaped. ,没有意义,但不需要逃脱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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