简体   繁体   English

格式化DateTime.Now

[英]Format DateTime.Now

A peculiar request this one. 一个特殊的要求。

I am looking to compare two times in C#. 我希望在C#中进行两次比较。

I have: 我有:

DateTime systemDate = DateTime.Now.AddMinutes(-15);
//which I presume subtracts 15 minutes from the current time

I am then reading in an ftp file from a Fedora server containing a date and time that I am reading in as a stream and converting to a DateTime 然后,我从Fedora服务器中读取ftp文件,该文件包含要作为流读取并转换为DateTime的日期和时间。

2011-03-17 09:00:18.000000000 +0000

I then parse this: 然后我解析:

compareDate = DateTime.Parse(streamer);

My Question therefore is how do I format systemDate to be in the same format as the one I have parsed in compareDate? 因此,我的问题是如何将systemDate的格式设置为与compareDate中解析的格式相同的格式? I am aware you can format DateTime but all of the sites I have seen so far dont have it in this format. 我知道您可以格式化DateTime,但是到目前为止,我所见过的所有网站都没有采用这种格式。

Thank you in advance, feel free to comment if you think I have missed anything :) 在此先感谢您,如果您认为我错过了任何事情,请随时发表评论:)

你可以这样

DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");

No need to format systemDate , because you already parsed the date from the FTP server into a DateTime . 无需格式化systemDate ,因为您已经将FTP服务器中的日期解析为DateTime You can just compare systemDate with compareDate . 您可以只将systemDatecompareDate进行比较。

just use var systemDate = DateTime.UtcNow.AddMinutes(-15); 只需使用var systemDate = DateTime.UtcNow.AddMinutes(-15); for your original one, then you just 为您的原始作品,那么您就

if (DateTime.Equals(systemDate, compareDate))
{
    //...
}

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

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