Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C语言中的日期差_C#_Vsto_Ms Project - Fatal编程技术网

C# C语言中的日期差

C# C语言中的日期差,c#,vsto,ms-project,C#,Vsto,Ms Project,使用VBA for MS Project时,其中一个命令是DateDifference differencedate = Application.DateDifference(tsk.BaselineStart, tsk.BaselineFinish, ActiveProject.Calendar) 然而,我尝试在MS项目中使用VSTO,但我知道如何使用DateDifference。是否有等效的命令cor C?Timespan有以下方法 // Gets the days

使用VBA for MS Project时,其中一个命令是DateDifference

differencedate = Application.DateDifference(tsk.BaselineStart, tsk.BaselineFinish, ActiveProject.Calendar)

然而,我尝试在MS项目中使用VSTO,但我知道如何使用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方法。将应用程序更改为MSProject.Application变量名称,并使用该变量为ActiveProject添加前缀。例如,如果变量名为ProjApp,请使用以下命令:

differencedate = ProjApp.DateDifference(tsk.BaselineStart, tsk.BaselineFinish, ProjApp.ActiveProject.Calendar)
返回的值将是两个日期之间的持续时间(以分钟为单位),与在VBA中使用时相同

一句话:如果您想要与在VBA中得到的计算相同,请使用此方法。如果您想要两个日期之间经过的时间,请使用C的内置日期数学函数。前者在处理由时间驱动的MS项目日期时最为相关

更新:我在C中对此进行了测试,发现DateDifference方法抛出了一个NotImplemented异常,但在vb.net中运行良好。由于此方法是准确计算两个日期之间持续时间的唯一方法,因此您可以使用vb.net在解决方案中添加单独的vb项目

更新2:下面是一个vb.net类,您可以从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));

在C中减去两个DateTime对象会得到一个时间跨度,其中包括以天、小时、分钟等为单位的差值。您能给出从文件中读取的值的代码吗?tsk.BaseLineStart/Finish是什么类型?tsk.BaseLineStart/Finish是日期,VBA上的命令Application.DateDifference对于MSProject的使用非常特定。tsk.BaselineStart>>>>2014年4月28日上午8:00:00 tsv.StartDate>>>>2014年4月28日在C中使用时,它们是DateTime对象,还是MS Project特有的某种office互操作类型?office互操作类型,在使用System.Convert.ToDateTime之前,我使用System.Convert.ToDateTime来确保它们是DateTime类型,然后使用Timespan timeDiff=Convert.ToDateTimetsk.BaseLineStart-Convert.ToDateTimetsk.BaselineFinish感谢您提供的信息。我只是有一个后续问题。使用datedifference和这些参数,tsk.BaselineStart>>28/04/14 8:00:00 AM,tsv.StartDate+7>>>05/05/14,答案是2400,如果我只减去日期,答案是6.16:00:00。。。2400是如何计算的?我认为Datedifference不仅仅是减去日期。这两个函数是相同的,只是参数略有不同。若要获取分钟数DateDiffstartDate、EndDate.Minute或DateDifferenceStartDate、FinishDate、Minuten,则DateDifference的最后一个参数是calendar,它返回自动计划任务的两个日期之间的持续时间(以分钟为单位)。不仅仅是减去日期。问题是,它只适用于VBA。嗨,是的,我知道日期是如何存储在内存中的。然而,datediff与datedifference不同。如果两者相同,则必须说明两者可以互换使用。DateDifference是专门为VBA MSProject创建的。嗨,rachel,我可以毫无错误地使用代码,但我没有得到任何结果。differencedate=mspApp.DateDifferenceSystem.Convert.ToDateTimetask.BaselineStart,System.Convert.ToDateTimetsv.StartDate.AddDays7,mspApp.ActiveProject.Calendar;我用的是C,有什么区别吗?谢谢。不要转换任务日期,按原样传递即可。试着不增加7天的开始时间,只是为了让它工作,然后解决增加7天的问题。是的,我已经做了,我提出了不同的解决方案,基本上我现在拥有的是:differencedate=mspApp.DateDifferencetask.BaselineStart,tsv.StartDate,mspApp.ActiveProject.Calendar/60;还是没有结果。。。我在这行后面有一个消息框,显示differencedate@GilbertAllanWong我添加了一些示例代码来帮助您开始。