C# 如何在UWP平台上使用ToOADate函数将DateTime值转换为OADate格式?

C# 如何在UWP平台上使用ToOADate函数将DateTime值转换为OADate格式?,c#,wpf,c#-4.0,uwp,uwp-xaml,C#,Wpf,C# 4.0,Uwp,Uwp Xaml,我正在WPF和WinForms平台中使用DateTime.fromooate()和DateTime.ToOADate()函数将DateTime值转换为oaate格式。但哪些功能在UWP平台中不可用。因此,任何人都可以建议您在UWP平台中将日期时间值转换为OADate格式 如何在UWP平台上使用ToOADate函数将DateTime值转换为OADate格式 您可以通过引用编写自己的方法。例如,下面是一个简单的演示,用于ToOADate和FromOADate函数,您可以对其进行测试 private

我正在WPF和WinForms平台中使用DateTime.fromooate()和DateTime.ToOADate()函数将DateTime值转换为oaate格式。但哪些功能在UWP平台中不可用。因此,任何人都可以建议您在UWP平台中将日期时间值转换为OADate格式

如何在UWP平台上使用ToOADate函数将DateTime值转换为OADate格式

您可以通过引用编写自己的方法。例如,下面是一个简单的演示,用于
ToOADate
FromOADate
函数,您可以对其进行测试

private void btndatetime_Click(object sender, RoutedEventArgs e)
{
    DateTime fromOA = FromOAdate(40967.6424503935);
    System.Diagnostics.Debug.WriteLine(fromOA);
    double toOA = ToOAdate(fromOA);
    System.Diagnostics.Debug.WriteLine(toOA);
}
public DateTime FromOAdate(double oadate)
{
    return new DateTime(DoubleDateToTicks(oadate), DateTimeKind.Unspecified);
} 
public double ToOAdate(DateTime datetime)
{
    return TicksToOADate(datetime.Ticks);
}
private const long TicksPerMillisecond = 10000;
private const long TicksPerSecond = TicksPerMillisecond * 1000;
private const long TicksPerMinute = TicksPerSecond * 60;
private const long TicksPerHour = TicksPerMinute * 60;
private const long TicksPerDay = TicksPerHour * 24;
private const UInt64 TicksMask = 0x3FFFFFFFFFFFFFFF;
private const long DoubleDateOffset = DaysTo1899 * TicksPerDay;
// Number of days in a non-leap year
private const int DaysPerYear = 365;
// Number of days in 4 years
private const int DaysPer4Years = DaysPerYear * 4 + 1;       // 1461
// Number of days in 100 years
private const int DaysPer100Years = DaysPer4Years * 25 - 1;  // 36524
// Number of days in 400 years
private const int DaysPer400Years = DaysPer100Years * 4 + 1; // 146097
private const int DaysTo1899 = DaysPer400Years * 4 + DaysPer100Years * 3 - 367;
// Number of milliseconds per time unit
private const int MillisPerSecond = 1000;
private const int MillisPerMinute = MillisPerSecond * 60;
private const int MillisPerHour = MillisPerMinute * 60;
private const int MillisPerDay = MillisPerHour * 24;
private static double TicksToOADate(long value)
{
    if (value == 0)
        return 0.0;  // Returns OleAut's zero'ed date value.
    if (value < TicksPerDay) // This is a fix for VB. They want the default day to be 1/1/0001 rathar then 12/30/1899.
        value += DoubleDateOffset; // We could have moved this fix down but we would like to keep the bounds check.          
    long millis = (value - DoubleDateOffset) / TicksPerMillisecond;
    if (millis < 0)
    {
        long frac = millis % MillisPerDay;
        if (frac != 0) millis -= (MillisPerDay + frac) * 2;
    }
    return (double)millis / MillisPerDay;
}
internal static long DoubleDateToTicks(double value)
{
    // The check done this way will take care of NaN
    //if (!(value < OADateMaxAsDouble) || !(value > OADateMinAsDouble))
    //    throw new ArgumentException(Environment.GetResourceString("Arg_OleAutDateInvalid"));
    // Conversion to long will not cause an overflow here, as at this point the "value" is in between OADateMinAsDouble and OADateMaxAsDouble
    long millis = (long)(value * MillisPerDay + (value >= 0 ? 0.5 : -0.5));
    // The interesting thing here is when you have a value like 12.5 it all positive 12 days and 12 hours from 01/01/1899
    // However if you a value of -12.25 it is minus 12 days but still positive 6 hours, almost as though you meant -11.75 all negative
    // This line below fixes up the millis in the negative case
    if (millis < 0)
    {
        millis -= (millis % MillisPerDay) * 2;
    }

    millis += DoubleDateOffset / TicksPerMillisecond;
    //if (millis < 0 || millis >= MaxMillis) throw new ArgumentException(Environment.GetResourceString("Arg_OleAutDateScale"));
    return millis * TicksPerMillisecond;
}
private void btndatime\u单击(对象发送方,路由目标)
{
DateTime fromOA=FromOAdate(40967.6424503935);
系统.诊断.调试.写线(fromOA);
双toOA=ToOAdate(fromOA);
系统诊断调试写线(toOA);
}
从oadate开始的公共日期时间(双oadate)
{
返回新的日期时间(DoubleDateToTicks(oadate),DateTimeKind.Unspecified);
} 
公共双ToOAdate(日期时间日期时间)
{
返回tickstoadate(datetime.Ticks);
}
私有常量长毫秒=10000;
private const long TicksPerSecond=tickspermillsecond*1000;
私有常量long TicksPerMinute=TicksPerSecond*60;
私人常数long TicksPerHour=TicksPerMinute*60;
private const long TicksPerDay=TicksPerHour*24;
私有const UInt64 TicksMask=0x3FFFFFFFFFFFFFFF;
private const long DoubleDateOffset=DaysTo1899*TicksPerDay;
//非闰年中的天数
private const int DaysPerYear=365;
//4年中的天数
private const int DaysPer4Years=DaysPerYear*4+1;//1461
//100年中的天数
私人建筑内日施工100年=日施工4年*25-1;//36524
//400年中的天数
私有常量int daysper400年=daysper100年*4+1;//146097
私人建筑国际日至1899年=日至400年*4+日至100年*3-367;
//每个时间单位的毫秒数
私有常量int毫秒每秒=1000;
私有常数int毫秒分钟=毫秒秒*60;
私有常量int MillisPerHour=毫秒分钟*60;
私有常量int MillisPerDay=MillisPerHour*24;
专用静态双字节(长值)
{
如果(值==0)
返回0.0;//返回OleAut的零日期值。
if(valueOADateMinAsDouble))
//抛出新的ArgumentException(Environment.GetResourceString(“Arg_OleAutDateInvalid”);
//在这里,转换为long不会导致溢出,因为此时“值”介于OADateMinAsDouble和OADateMaxAsDouble之间
长密度=(长)(值*密度日+(值>=0?0.5:-0.5));
//有趣的是,当你有一个像12.5这样的值时,它从1899年1月1日起12天12小时都是正的
//但是,如果你的值为-12.25,那么它是负12天,但仍然是正6小时,几乎就像你的意思是-11.75都是负的一样
//下面这一行修正了负片中的毫秒
如果(毫秒<0)
{
毫秒-=(毫秒百分比毫秒日)*2;
}
毫秒+=双日期偏移量/毫秒;
//如果(millis<0 | | millis>=MaxMillis)抛出新的ArgumentException(Environment.GetResourceString(“Arg_OleAutDateScale”);
返回毫秒*毫秒;
}

感谢您的回复。我已经测试了这个解决方案,效果非常好。