如何检查两个日期时间是在同一个月和一年 [英] How to check two date time are in same month and year

查看:243
本文介绍了如何检查两个日期时间是在同一个月和一年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查两个日期时间是否在同一年的同一个月



ex:'2014-01-01' - '2014 -01-25'都在2014年1月

there are two date time i need to check whether they are in same month of same year

ex:'2014-01-01'--'2014-01-25' both are in january 2014

推荐答案

您可以使用System.DateTime类。 http://msdn.microsoft.com/en-us /library/system.datetime.month(v=vs.110).aspx [ ^ ]



它有示例代码。您可以为2个日期创建2个新的System.DateTime实例,然后比较dateTime1.Month和dateTime2.Month以及.Year。



因为它们是日期,还有很多其他方法可以做到这一点,但这将是一个简单的方法。
You can use the System.DateTime class. http://msdn.microsoft.com/en-us/library/system.datetime.month(v=vs.110).aspx[^]

It has example code in there. You can create 2 new instances of System.DateTime of your 2 dates and then compare dateTime1.Month and dateTime2.Month as well as .Year on both.

Because they are dates, there are also many other ways you can do this but this would be a simple way.


DateTime d1 = new DateTime(2014, 9, 19, 7, 0, 0); // 2014-09-19 7:00:00
DateTime d2 = new DateTime(2014, 9, 25, 7, 0, 0); // 2014-09-25 7:00:00

if (d1.Month == d2.Month && d1.Year == d2.Year)
{
    // do what you need
}


这篇关于如何检查两个日期时间是在同一个月和一年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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