比较两个日期的字符串格式 [英] Compare two dates which is in string format

查看:114
本文介绍了比较两个日期的字符串格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的先生

我想比较dd/MM/YYYY字符串格式的两个日期

Ankita Mali

Dear Sir

I want to compare two dates which is in dd/MM/YYYY string format

Ankita Mali

推荐答案

您可以使用 DateTime. Parse() [ ^ ]方法将字符串转换为DateTime值,然后进行比较,非常简单.
You can use DateTime.Parse()[^] method to convert the string to a DateTime value and then after that, the compare is pretty straightforward.


只需将字符串转换为DateTime对象,然后使用==运算符比较它们:

Simply convert the strings to DateTime objects, and then use the == operator to compare them:

DateTime dt1;
DateTime dt2;

if (DateTime.TryParse(strDate1, out dt1) && DateTime.TryParse(strDate2, out dt2))
{
    if (dt1.Date == dt2.Date)
    {
        // the dates are identical
    }
}


将它们都转换为DateTime,然后使用DateTime.Compare进行比较
Convert both to DateTime and then use DateTime.Compare to do the comparison


这篇关于比较两个日期的字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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