2个日期之间的月份差 [英] Month difference between 2 dates

查看:58
本文介绍了2个日期之间的月份差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算一个日期列与今天之间的MONTH差.我们在csharp中是否有任何方法(例如monthdiff或datediff)来实现此功能?我的代码的问题是,如果提交日期年份不同,则会中断.

I am trying to calculate MONTH difference between one date column and today. Do we have any method in csharp like monthdiff or datediff to achieve this functionlaity? Issue with my code is if submission date year is different then it breaks.

bool isDateAccepted = ((SubmissionDate.Month - DateTime.Now.Month) < 6)

推荐答案

您总是可以在提交日期前添加6个月,然后将其与当前日期进行比较.

You could always add 6 months to the submission date, and compare it to the current date.

bool isDateAccepted = (submissionDate.AddMonths(6) > DateTime.Now);

这篇关于2个日期之间的月份差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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