Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.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# 如何将DateTime.Now转换为c中的0到1十进制格式_C#_Datetime_Timezone_Format_Decimal - Fatal编程技术网

C# 如何将DateTime.Now转换为c中的0到1十进制格式

C# 如何将DateTime.Now转换为c中的0到1十进制格式,c#,datetime,timezone,format,decimal,C#,Datetime,Timezone,Format,Decimal,我想将当前时间转换为表示一天的小数点。例如,如果一天从0开始,那么12:00 PM应该是0.5 我需要将该值发送到API,并且它需要采用该格式。i、 e "LAST_PRINT_TIME":0.22020833" 根据结果的精度要求,这可能有助于您: DateTime now = DateTime.Now; double dayFraction = (now.Hour + now.Minute / 60d) / 24d; 现在.Minute/60d计算当前小时的分数,因此如果时间为XX:15

我想将当前时间转换为表示一天的小数点。例如,如果一天从0开始,那么12:00 PM应该是0.5

我需要将该值发送到API,并且它需要采用该格式。i、 e

"LAST_PRINT_TIME":0.22020833"

根据结果的精度要求,这可能有助于您:

DateTime now = DateTime.Now;
double dayFraction = (now.Hour + now.Minute / 60d) / 24d;
现在.Minute/60d计算当前小时的分数,因此如果时间为XX:15 PM,则该值为0.25。然后将其添加到当前小时。然后将该值除以24得到最终结果

例如,下午3:45的时间如下: 15+45/60/24=>15+0.75/24=>15.75/24=>0.65625

因此,下午3:45,即一天中的15.75小时,将是一天中的0.65625或65.625%

或者,正如@madreflection在评论中提到的,您也可以使用.ToOADate。在这种情况下,您可以执行以下操作:

DateTime now = DateTime.Now;
double dayFraction = now.ToOADate() - now.Date.ToOADate();

根据结果的精度要求,这可能有助于您:

DateTime now = DateTime.Now;
double dayFraction = (now.Hour + now.Minute / 60d) / 24d;
现在.Minute/60d计算当前小时的分数,因此如果时间为XX:15 PM,则该值为0.25。然后将其添加到当前小时。然后将该值除以24得到最终结果

例如,下午3:45的时间如下: 15+45/60/24=>15+0.75/24=>15.75/24=>0.65625

因此,下午3:45,即一天中的15.75小时,将是一天中的0.65625或65.625%

或者,正如@madreflection在评论中提到的,您也可以使用.ToOADate。在这种情况下,您可以执行以下操作:

DateTime now = DateTime.Now;
double dayFraction = now.ToOADate() - now.Date.ToOADate();

这是一个看似简单的问题,但解决方案实际上比你想象的要复杂得多

这种复杂性源于当地时间的性质,当地时间的规则是由时区定义的。许多时区具有定期发生的转换,例如,或不定期发生的转换,例如标准时间的变化

因此,需要考虑:

一天能短于或长于24小时吗? 例如,在我们大多数人中,DST的开始时间是凌晨2:00,而在这一天有23个小时,因为从2:00到2:59的时间被跳过。在夏令时结束时,也就是美国的凌晨2:00,从1:00到1:59的时间被重复,这一天创造了25个小时。 除了午夜,一天可以开始还是停止? 例如,DST的开始使日期2019-09-08从01:00开始,而不是00:00。 在中了解更多信息

考虑使用以下方法来克服这些现实问题

首先,定义一些助手函数来完成大部分工作。它们不是特定于特定时间点或特定时区的

static double GetFractionOfDay(DateTimeOffset dto, TimeZoneInfo tz)
{
    // Get the start of the day, and the start of the next day
    DateTimeOffset startOfDay = GetStartOfDay(dto, tz);
    DateTimeOffset startOfNextDay = GetStartOfDay(startOfDay.AddDays(1), tz);

    // Calculate the length of the day.  It might not be 24 hours!
    TimeSpan lengthOfDay = startOfNextDay - startOfDay;

    // Now calculate the position within the day, and the fraction to return
    TimeSpan durationSinceStartOfDay = dto - startOfDay;
    return durationSinceStartOfDay / lengthOfDay;
}

static DateTimeOffset GetStartOfDay(DateTimeOffset dto, TimeZoneInfo tz)
{
    // Make sure we're in the correct time zone
    dto = TimeZoneInfo.ConvertTime(dto, tz);

    // Start by assuming a local midnight exists
    DateTime dt = dto.Date;

    // Handle days without a local midnight (these do exist in several time zones)
    if (tz.IsInvalidTime(dt))
    {
        // Advance by the transition gap.  This is usually 1 hour, but best not to hard-code that.
        TimeSpan[] offsets = { tz.GetUtcOffset(dt.AddDays(-1)), tz.GetUtcOffset(dt.AddDays(1)) };
        TimeSpan gap = offsets[1] - offsets[0];
        return new DateTimeOffset(dt.Add(gap), offsets[1]);
    }

    // Handle days with more than one midnight (it's possible, even if unlikely)
    if (tz.IsAmbiguousTime(dt))
    {
        // There's more than one.  Prefer the first one, since we want the beginning of the day.
        TimeSpan[] offsets = tz.GetAmbiguousTimeOffsets(dt);
        TimeSpan offset = offsets[0] > offsets[1] ? offsets[0] : offsets[1];
        return new DateTimeOffset(dt, offset);
    }

    // Clean case, just one local midnight and it does exist
    return new DateTimeOffset(dt, tz.GetUtcOffset(dt));
}
double dayFraction = GetFractionOfDay(DateTimeOffset.Now, TimeZoneInfo.Local);
有了这些定义,您现在就可以在本地时区中获得有关now的问题的答案

static double GetFractionOfDay(DateTimeOffset dto, TimeZoneInfo tz)
{
    // Get the start of the day, and the start of the next day
    DateTimeOffset startOfDay = GetStartOfDay(dto, tz);
    DateTimeOffset startOfNextDay = GetStartOfDay(startOfDay.AddDays(1), tz);

    // Calculate the length of the day.  It might not be 24 hours!
    TimeSpan lengthOfDay = startOfNextDay - startOfDay;

    // Now calculate the position within the day, and the fraction to return
    TimeSpan durationSinceStartOfDay = dto - startOfDay;
    return durationSinceStartOfDay / lengthOfDay;
}

static DateTimeOffset GetStartOfDay(DateTimeOffset dto, TimeZoneInfo tz)
{
    // Make sure we're in the correct time zone
    dto = TimeZoneInfo.ConvertTime(dto, tz);

    // Start by assuming a local midnight exists
    DateTime dt = dto.Date;

    // Handle days without a local midnight (these do exist in several time zones)
    if (tz.IsInvalidTime(dt))
    {
        // Advance by the transition gap.  This is usually 1 hour, but best not to hard-code that.
        TimeSpan[] offsets = { tz.GetUtcOffset(dt.AddDays(-1)), tz.GetUtcOffset(dt.AddDays(1)) };
        TimeSpan gap = offsets[1] - offsets[0];
        return new DateTimeOffset(dt.Add(gap), offsets[1]);
    }

    // Handle days with more than one midnight (it's possible, even if unlikely)
    if (tz.IsAmbiguousTime(dt))
    {
        // There's more than one.  Prefer the first one, since we want the beginning of the day.
        TimeSpan[] offsets = tz.GetAmbiguousTimeOffsets(dt);
        TimeSpan offset = offsets[0] > offsets[1] ? offsets[0] : offsets[1];
        return new DateTimeOffset(dt, offset);
    }

    // Clean case, just one local midnight and it does exist
    return new DateTimeOffset(dt, tz.GetUtcOffset(dt));
}
double dayFraction = GetFractionOfDay(DateTimeOffset.Now, TimeZoneInfo.Local);

然而,尽管这是一天中哪一部分时间的正确答案,但请记住,与接收API的期望保持一致可能更重要,即使不完全正确。换句话说,如果12:00应该始终为0.5,即使它不在一天的中点,那么使用方法。

这是一个看似简单的问题,但解决方案实际上比你想象的要复杂得多

这种复杂性源于当地时间的性质,当地时间的规则是由时区定义的。许多时区具有定期发生的转换,例如,或不定期发生的转换,例如标准时间的变化

因此,需要考虑:

一天能短于或长于24小时吗? 例如,在我们大多数人中,DST的开始时间是凌晨2:00,而在这一天有23个小时,因为从2:00到2:59的时间被跳过。在夏令时结束时,也就是美国的凌晨2:00,从1:00到1:59的时间被重复,这一天创造了25个小时。 除了午夜,一天可以开始还是停止? 例如,DST的开始使日期2019-09-08从01:00开始,而不是00:00。 在中了解更多信息

考虑使用以下方法来克服这些现实问题

首先,定义一些助手函数来完成大部分工作。它们不是特定于特定时间点或特定时区的

static double GetFractionOfDay(DateTimeOffset dto, TimeZoneInfo tz)
{
    // Get the start of the day, and the start of the next day
    DateTimeOffset startOfDay = GetStartOfDay(dto, tz);
    DateTimeOffset startOfNextDay = GetStartOfDay(startOfDay.AddDays(1), tz);

    // Calculate the length of the day.  It might not be 24 hours!
    TimeSpan lengthOfDay = startOfNextDay - startOfDay;

    // Now calculate the position within the day, and the fraction to return
    TimeSpan durationSinceStartOfDay = dto - startOfDay;
    return durationSinceStartOfDay / lengthOfDay;
}

static DateTimeOffset GetStartOfDay(DateTimeOffset dto, TimeZoneInfo tz)
{
    // Make sure we're in the correct time zone
    dto = TimeZoneInfo.ConvertTime(dto, tz);

    // Start by assuming a local midnight exists
    DateTime dt = dto.Date;

    // Handle days without a local midnight (these do exist in several time zones)
    if (tz.IsInvalidTime(dt))
    {
        // Advance by the transition gap.  This is usually 1 hour, but best not to hard-code that.
        TimeSpan[] offsets = { tz.GetUtcOffset(dt.AddDays(-1)), tz.GetUtcOffset(dt.AddDays(1)) };
        TimeSpan gap = offsets[1] - offsets[0];
        return new DateTimeOffset(dt.Add(gap), offsets[1]);
    }

    // Handle days with more than one midnight (it's possible, even if unlikely)
    if (tz.IsAmbiguousTime(dt))
    {
        // There's more than one.  Prefer the first one, since we want the beginning of the day.
        TimeSpan[] offsets = tz.GetAmbiguousTimeOffsets(dt);
        TimeSpan offset = offsets[0] > offsets[1] ? offsets[0] : offsets[1];
        return new DateTimeOffset(dt, offset);
    }

    // Clean case, just one local midnight and it does exist
    return new DateTimeOffset(dt, tz.GetUtcOffset(dt));
}
double dayFraction = GetFractionOfDay(DateTimeOffset.Now, TimeZoneInfo.Local);
有了这些定义,您现在就可以在本地时区中获得有关now的问题的答案

static double GetFractionOfDay(DateTimeOffset dto, TimeZoneInfo tz)
{
    // Get the start of the day, and the start of the next day
    DateTimeOffset startOfDay = GetStartOfDay(dto, tz);
    DateTimeOffset startOfNextDay = GetStartOfDay(startOfDay.AddDays(1), tz);

    // Calculate the length of the day.  It might not be 24 hours!
    TimeSpan lengthOfDay = startOfNextDay - startOfDay;

    // Now calculate the position within the day, and the fraction to return
    TimeSpan durationSinceStartOfDay = dto - startOfDay;
    return durationSinceStartOfDay / lengthOfDay;
}

static DateTimeOffset GetStartOfDay(DateTimeOffset dto, TimeZoneInfo tz)
{
    // Make sure we're in the correct time zone
    dto = TimeZoneInfo.ConvertTime(dto, tz);

    // Start by assuming a local midnight exists
    DateTime dt = dto.Date;

    // Handle days without a local midnight (these do exist in several time zones)
    if (tz.IsInvalidTime(dt))
    {
        // Advance by the transition gap.  This is usually 1 hour, but best not to hard-code that.
        TimeSpan[] offsets = { tz.GetUtcOffset(dt.AddDays(-1)), tz.GetUtcOffset(dt.AddDays(1)) };
        TimeSpan gap = offsets[1] - offsets[0];
        return new DateTimeOffset(dt.Add(gap), offsets[1]);
    }

    // Handle days with more than one midnight (it's possible, even if unlikely)
    if (tz.IsAmbiguousTime(dt))
    {
        // There's more than one.  Prefer the first one, since we want the beginning of the day.
        TimeSpan[] offsets = tz.GetAmbiguousTimeOffsets(dt);
        TimeSpan offset = offsets[0] > offsets[1] ? offsets[0] : offsets[1];
        return new DateTimeOffset(dt, offset);
    }

    // Clean case, just one local midnight and it does exist
    return new DateTimeOffset(dt, tz.GetUtcOffset(dt));
}
double dayFraction = GetFractionOfDay(DateTimeOffset.Now, TimeZoneInfo.Local);

然而,尽管这是一天中哪一部分时间的正确答案,但请记住,与接收API的期望保持一致可能更重要,即使不完全正确。换句话说,如果12:00应该始终是0.5,即使它不完全在一天的中点,那么使用方法。

我的意思是,如果一天从0开始,12:00应该是。5:12:00的格式模棱两可,但我理解你的意思,24小时=1-我很好奇闰秒在这个世界上是如何工作的?Weclome to StackOverflow。请花些时间阅读,然后提供一份说明您的问题的报告

xcel使用这种格式。日期是十进制分隔符前面的值,小时到秒是后面的值。DateTime的ToOADate方法将返回一个双倍值,其中午夜是一个整数,中午是x.5,只需减去整部分。我的意思是,如果一天从0开始,下午12点应该是。5格式12pm是模糊的,但我理解你的意思,24小时=1-我很好奇闰秒在这个世界上是怎么工作的?我们来看看StackOverflow。请花些时间阅读,然后提供一个显示您的问题的表格。IIRC,Excel使用这种格式。日期是小数分隔符前面的值,小时到秒是后面的值。DateTime的ToOADate方法将返回一个双精度值,其中午夜为整数,中午为x.5,只需减去整部分即可。为了尽量减少浮点错误,您可以使用now.Date.ToOADate进行减法,而不必使用Math.Floor或Math.Truncate。@这是一个很好的观点,我将进行编辑以包含。如果需要的话,也可以随意编辑,但我想让原作者有机会发表自己的意见,直到他们完成。不,保留它。我没问题。@elmer007看起来这很有效,非常感谢。为了尽量减少浮点错误,您可以现在减去.Date.ToOADate,而不是使用Math.Floor或Math.Truncate。@madreflection很好,我将编辑为include。如果需要的话,也可以随意编辑,但我想让原作者有机会发表自己的意见,直到他们完成。不,保留它。我没问题。@elmer007看起来这已经成功了,非常感谢。Jonson-Pint这也是一个很好的信息,我将分析代码。谢谢你,这也是一个很好的信息,我将分析代码。谢谢