计算两个日期之间的月数

编程入门 行业动态 更新时间:2024-10-08 12:40:38
本文介绍了计算两个日期之间的月数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下VB.NET代码:

I have the following VB.NET Code:

Dim Date1 As New DateTime(2010,5,6) Dim Date2 As New DateTime(2009,10,12) Dim NumOfMonths = 0 ' This is where I am stumped

我要做的是找出两个日期之间有多少个月。任何帮助将不胜感激。

What I am trying to do is find out how many months are between the 2 dates. Any help would be appreciated.

推荐答案

以下是您可以使用的方法:

Here's a method you could use:

Public Shared Function MonthDifference(ByVal first As DateTime, ByVal second As DateTime) As Integer Return Math.Abs((first.Month - second.Month) + 12 * (first.Year - second.Year)) End Function

像这样:

Dim Date1 As New DateTime(2010,5,6) Dim Date2 As New DateTime(2009,10,12) Dim NumOfMonths = MonthDifference(Date1, Date2)

更多推荐

计算两个日期之间的月数

本文发布于:2023-10-30 18:36:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1543675.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   日期

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!