如何删除DateTime.ParseExact中的秒 [英] how to remove seconds in the DateTime.ParseExact

查看:104
本文介绍了如何删除DateTime.ParseExact中的秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的下面的代码中我想从日期中删除秒。即使我给了字符串格式的秒数,但DateTime.ParseExact给出的时间与秒(25:50:00)00。如何我可以解决这个问题,以便我的时间字符串随时出现





dateString =08/14/2013 12:25 PM

format =MM / dd / yyyy hh:mm tt



result = DateTime.ParseExact(dateString,format,provider );

string changedDt = result.ToString(CultureInfo.CurrentCulture.DateTimeFormat);

Console.WriteLine(changedDt}

解决方案

DateTime在内部存储为ticks。当你创建一个datetime时,总会有几秒(甚至几毫秒)存储在结构中。你不能改变它。在DateTime上使用不同的方法,比如.Today给出日期部分,但它的其他部分是零。这是设计的。



如果您不想在创建字符串时显示秒数,请使用格式字符串,如MM / dd / yyyy hh.mm关闭tt部分。


你怎么知道如何使用 System.DateTime 的字符串格式进行解析并且不能这样做 ToString



请参阅: http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx [ ^ ]。



此方法,参数是格式。请参阅:

http://msdn.microsoft.com/en-us /library/az4se3k1.aspx [ ^ ],

http://msdn.microsoft.com/en -us / library / 8kb3ddd4.aspx [ ^ ]。



使用自定义格式字符串,包括小时和分钟,但不包括秒('s','ss'),就像它的已经用您的格式字符串 format 完成。



-SA

hi in my below code i want to remove the Seconds from the date.Even I gave string format with out seconds but the DateTime.ParseExact is giving the time with the seconds(25:50:00)"00".How can i resolve this issue so that my time string comes with out any seconds


dateString = "08/14/2013 12:25 PM"
format = "MM/dd/yyyy hh:mm tt"

result = DateTime.ParseExact(dateString, format, provider);
string alteredDt = result.ToString(CultureInfo.CurrentCulture.DateTimeFormat);
Console.WriteLine(alteredDt}

解决方案

DateTime is stored internally as ticks. When you create a datetime, there are always seconds (even milliseconds) that are stored in the structure. You can't change this. Using different methods on the DateTime, like .Today give the date part, but the other parts of it are zero. This is by design.

If you don't want to display the seconds when you create the string, use a format string, like MM/dd/yyyy hh.mm and leave the tt part off.


How come you know how to use the string format for System.DateTime for parsing and fail to do so for ToString?

Please see: http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx[^].

In this method, the parameter is the format. Please see:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

Use custom format string and include hours and minutes, but not seconds ('s', 'ss'), pretty much as it's already done in your format string "format".

—SA


这篇关于如何删除DateTime.ParseExact中的秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆