C# 中的日期差
DateDifference in C#
对 MS 项目使用 VBA,命令之一是 DateDifference。
differencedate = Application.DateDifference(tsk.BaselineStart, tsk.BaselineFinish, ActiveProject.Calendar)
但是,我正在尝试将 VSTO 用于 MS Project,但我可以弄清楚如何使用 DateDifference。是否有等效的命令 cor C#?
Timespan 有以下方法
// Gets the days component of the time interval represented by the current System.TimeSpan
// structure.
//
// Returns:
// The day component of this instance. The return value can be positive or negative.
public int Days { get; }
// Summary:
// Gets the hours component of the time interval represented by the current
// System.TimeSpan structure.
//
// Returns:
// The hour component of the current System.TimeSpan structure. The return value
// ranges from -23 through 23.
public int Hours { get; }
// Summary:
// Gets the milliseconds component of the time interval represented by the current
// System.TimeSpan structure.
//
// Returns:
// The millisecond component of the current System.TimeSpan structure. The return
// value ranges from -999 through 999.
public int Milliseconds { get; }
// Summary:
// Gets the minutes component of the time interval represented by the current
// System.TimeSpan structure.
//
// Returns:
// The minute component of the current System.TimeSpan structure. The return
// value ranges from -59 through 59.
public int Minutes { get; }
// Summary:
// Gets the seconds component of the time interval represented by the current
// System.TimeSpan structure.
//
// Returns:
// The second component of the current System.TimeSpan structure. The return
// value ranges from -59 through 59.
public int Seconds { get; }
// Summary:
// Gets the number of ticks that represent the value of the current System.TimeSpan
// structure.
//
// Returns:
// The number of ticks contained in this instance.
public long Ticks { get; }
// Summary:
// Gets the value of the current System.TimeSpan structure expressed in whole
// and fractional days.
//
// Returns:
// The total number of days represented by this instance.
public double TotalDays { get; }
// Summary:
// Gets the value of the current System.TimeSpan structure expressed in whole
// and fractional hours.
//
// Returns:
// The total number of hours represented by this instance.
public double TotalHours { get; }
// Summary:
// Gets the value of the current System.TimeSpan structure expressed in whole
// and fractional milliseconds.
//
// Returns:
// The total number of milliseconds represented by this instance.
public double TotalMilliseconds { get; }
// Summary:
// Gets the value of the current System.TimeSpan structure expressed in whole
// and fractional minutes.
//
// Returns:
// The total number of minutes represented by this instance.
public double TotalMinutes { get; }
// Summary:
// Gets the value of the current System.TimeSpan structure expressed in whole
// and fractional seconds.
//
// Returns:
// The total number of seconds represented by this instance.
public double TotalSeconds { get; }
日期存储为数字,从 1900 年 1 月 1 日开始的天数是数字的整数部分,数字的小数部分是一天的小数部分。当显示数字时,它可能会有所不同,具体取决于您用于转换为字符串的方法。 VBA 和 Net Library 在内存中存储相同的数字。有些方法只会显示整数,有些方法只会显示数字的小数部分,而其他方法会显示整个数字。显示也可能因国家/地区而异。 6.16 可能是 6 天、6 小时、6 分钟或 6 秒。它可以是整个数字,只是数字的小数部分,甚至是分数的更小部分。通过简单地添加或减去存储在内存中的数字来执行数学运算。希望正确对齐。
您可以在 vsto 中使用完全相同的 MS Project VBA 方法。将 Application
更改为您的 MSProject.Application 变量名称,并在 ActiveProject
前加上该变量。例如,如果您的变量名称是 ProjApp
,请使用:
differencedate = ProjApp.DateDifference(tsk.BaselineStart, tsk.BaselineFinish, ProjApp.ActiveProject.Calendar)
返回值将是日期之间的持续时间(以分钟为单位),与您在 VBA 中使用时相同。
底线:如果您想要与VBA中的计算相同,请使用此方法。如果您想要日期之间经过的时间,请使用 C# 的内置日期数学函数。前者在处理 cpm 驱动的 MS 项目日期时最相关。
Update:我在 C# 中对此进行了测试,发现 DateDifference
方法会引发 NotImplemented
异常(但在 vb.net 中工作正常).由于此方法是准确计算两个日期之间的持续时间的唯一方法,因此您可以仅针对此部分使用 vb.net,在您的解决方案中添加一个单独的 (vb) 项目。
更新 2:这是一个 vb.net class 你可以从 c# 调用:
Imports Microsoft.Office.Interop
Public Class MsProjectMethods
Public Function MsProjectDateDifference(ByVal ProjApp As MSProject.Application,
ByVal startDate As DateTime,
ByVal finishDate As DateTime) As Int32
Dim returnValue As Object = ProjApp.DateDifference(startDate, finishDate)
If IsNumeric(returnValue) Then
Return Convert.ToInt32(returnValue)
Else
Throw New System.Exception("An exception has occurred.")
End If
End Function
End Class
您可以这样称呼它:
ClassLibrary1.MsProjectMethods MspVb = new ClassLibrary1.MsProjectMethods();
int differencedate = MspVb.MsProjectDateDifference(ProjApp,
Convert.ToDateTime(tsk.BaselineStart),
Convert.ToDateTime(tsk.BaselineFinish));
对 MS 项目使用 VBA,命令之一是 DateDifference。
differencedate = Application.DateDifference(tsk.BaselineStart, tsk.BaselineFinish, ActiveProject.Calendar)
但是,我正在尝试将 VSTO 用于 MS Project,但我可以弄清楚如何使用 DateDifference。是否有等效的命令 cor C#?
Timespan 有以下方法
// Gets the days component of the time interval represented by the current System.TimeSpan
// structure.
//
// Returns:
// The day component of this instance. The return value can be positive or negative.
public int Days { get; }
// Summary:
// Gets the hours component of the time interval represented by the current
// System.TimeSpan structure.
//
// Returns:
// The hour component of the current System.TimeSpan structure. The return value
// ranges from -23 through 23.
public int Hours { get; }
// Summary:
// Gets the milliseconds component of the time interval represented by the current
// System.TimeSpan structure.
//
// Returns:
// The millisecond component of the current System.TimeSpan structure. The return
// value ranges from -999 through 999.
public int Milliseconds { get; }
// Summary:
// Gets the minutes component of the time interval represented by the current
// System.TimeSpan structure.
//
// Returns:
// The minute component of the current System.TimeSpan structure. The return
// value ranges from -59 through 59.
public int Minutes { get; }
// Summary:
// Gets the seconds component of the time interval represented by the current
// System.TimeSpan structure.
//
// Returns:
// The second component of the current System.TimeSpan structure. The return
// value ranges from -59 through 59.
public int Seconds { get; }
// Summary:
// Gets the number of ticks that represent the value of the current System.TimeSpan
// structure.
//
// Returns:
// The number of ticks contained in this instance.
public long Ticks { get; }
// Summary:
// Gets the value of the current System.TimeSpan structure expressed in whole
// and fractional days.
//
// Returns:
// The total number of days represented by this instance.
public double TotalDays { get; }
// Summary:
// Gets the value of the current System.TimeSpan structure expressed in whole
// and fractional hours.
//
// Returns:
// The total number of hours represented by this instance.
public double TotalHours { get; }
// Summary:
// Gets the value of the current System.TimeSpan structure expressed in whole
// and fractional milliseconds.
//
// Returns:
// The total number of milliseconds represented by this instance.
public double TotalMilliseconds { get; }
// Summary:
// Gets the value of the current System.TimeSpan structure expressed in whole
// and fractional minutes.
//
// Returns:
// The total number of minutes represented by this instance.
public double TotalMinutes { get; }
// Summary:
// Gets the value of the current System.TimeSpan structure expressed in whole
// and fractional seconds.
//
// Returns:
// The total number of seconds represented by this instance.
public double TotalSeconds { get; }
日期存储为数字,从 1900 年 1 月 1 日开始的天数是数字的整数部分,数字的小数部分是一天的小数部分。当显示数字时,它可能会有所不同,具体取决于您用于转换为字符串的方法。 VBA 和 Net Library 在内存中存储相同的数字。有些方法只会显示整数,有些方法只会显示数字的小数部分,而其他方法会显示整个数字。显示也可能因国家/地区而异。 6.16 可能是 6 天、6 小时、6 分钟或 6 秒。它可以是整个数字,只是数字的小数部分,甚至是分数的更小部分。通过简单地添加或减去存储在内存中的数字来执行数学运算。希望正确对齐。
您可以在 vsto 中使用完全相同的 MS Project VBA 方法。将 Application
更改为您的 MSProject.Application 变量名称,并在 ActiveProject
前加上该变量。例如,如果您的变量名称是 ProjApp
,请使用:
differencedate = ProjApp.DateDifference(tsk.BaselineStart, tsk.BaselineFinish, ProjApp.ActiveProject.Calendar)
返回值将是日期之间的持续时间(以分钟为单位),与您在 VBA 中使用时相同。
底线:如果您想要与VBA中的计算相同,请使用此方法。如果您想要日期之间经过的时间,请使用 C# 的内置日期数学函数。前者在处理 cpm 驱动的 MS 项目日期时最相关。
Update:我在 C# 中对此进行了测试,发现 DateDifference
方法会引发 NotImplemented
异常(但在 vb.net 中工作正常).由于此方法是准确计算两个日期之间的持续时间的唯一方法,因此您可以仅针对此部分使用 vb.net,在您的解决方案中添加一个单独的 (vb) 项目。
更新 2:这是一个 vb.net class 你可以从 c# 调用:
Imports Microsoft.Office.Interop
Public Class MsProjectMethods
Public Function MsProjectDateDifference(ByVal ProjApp As MSProject.Application,
ByVal startDate As DateTime,
ByVal finishDate As DateTime) As Int32
Dim returnValue As Object = ProjApp.DateDifference(startDate, finishDate)
If IsNumeric(returnValue) Then
Return Convert.ToInt32(returnValue)
Else
Throw New System.Exception("An exception has occurred.")
End If
End Function
End Class
您可以这样称呼它:
ClassLibrary1.MsProjectMethods MspVb = new ClassLibrary1.MsProjectMethods();
int differencedate = MspVb.MsProjectDateDifference(ProjApp,
Convert.ToDateTime(tsk.BaselineStart),
Convert.ToDateTime(tsk.BaselineFinish));