C# 通过特定方式将datetime设置为特定日期

C# 通过特定方式将datetime设置为特定日期,c#,asp.net,datetime,c#-4.0,C#,Asp.net,Datetime,C# 4.0,我有一个带有日期时间的变量,我必须通过以下规则和场景在特定日期设置该变量: 我连接的API有一个每日限制,一旦达到该限制,我必须等到第二天早上9:10,CEST听起来你真正想说的是: 查找中欧的当前时间 同一天上午9点10分 如果上午9:10在当前时间之后,则添加一天 比如: // No need to do this more than once private static readonly TimeZoneInfo centralEuropeZone = TimeZoneI

我有一个带有日期时间的变量,我必须通过以下规则和场景在特定日期设置该变量:


  • 我连接的API有一个每日限制,一旦达到该限制,我必须等到第二天早上9:10,CEST听起来你真正想说的是:

    • 查找中欧的当前时间
    • 同一天上午9点10分
    • 如果上午9:10在当前时间之后,则添加一天
    比如:

    // No need to do this more than once
    private static readonly TimeZoneInfo centralEuropeZone = 
        TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time")
    
    private static DateTime GetUtcResetTime()
    {
        // Using UtcNow to make it clear that the system time zone is irrelevant
        var centralEuropeNow = TimeZoneInfo.ConvertTime(DateTime.UtcNow, centralEuropeZone);
        var centralEuropeResetTime = centralEuropeNow.Date + new TimeSpan(9, 10, 0);
        if (centralEuropeResetTime <= centralEuropeNow)
        {
            centralEuropeResetTime = centralEuropeResetTime.AddDays(1);
        }
        return TimeZoneInfo.ConvertTimeToUtc(centralEuropeResetTime, centralEuropeZone);
    }
    
    //无需多次执行此操作
    专用静态只读时区信息中心时区=
    TimeZoneInfo.FindSystemTimeZoneById(“中欧标准时间”)
    私有静态日期时间GetUtcResetTime()
    {
    //使用UtcNow明确说明系统时区无关
    var centralEuropeNow=TimeZoneInfo.ConvertTime(DateTime.UtcNow,centralEuropeZone);
    var centralEuropeResetTime=centralEuropeNow.Date+new TimeSpan(9,10,0);
    
    如果(centralEuropeResetTime听起来你真正想说的是:

    • 查找中欧的当前时间
    • 同一天上午9点10分
    • 如果上午9:10在当前时间之后,则添加一天
    比如:

    // No need to do this more than once
    private static readonly TimeZoneInfo centralEuropeZone = 
        TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time")
    
    private static DateTime GetUtcResetTime()
    {
        // Using UtcNow to make it clear that the system time zone is irrelevant
        var centralEuropeNow = TimeZoneInfo.ConvertTime(DateTime.UtcNow, centralEuropeZone);
        var centralEuropeResetTime = centralEuropeNow.Date + new TimeSpan(9, 10, 0);
        if (centralEuropeResetTime <= centralEuropeNow)
        {
            centralEuropeResetTime = centralEuropeResetTime.AddDays(1);
        }
        return TimeZoneInfo.ConvertTimeToUtc(centralEuropeResetTime, centralEuropeZone);
    }
    
    //无需多次执行此操作
    专用静态只读时区信息中心时区=
    TimeZoneInfo.FindSystemTimeZoneById(“中欧标准时间”)
    私有静态日期时间GetUtcResetTime()
    {
    //使用UtcNow明确说明系统时区无关
    var centralEuropeNow=TimeZoneInfo.ConvertTime(DateTime.UtcNow,centralEuropeZone);
    var centralEuropeResetTime=centralEuropeNow.Date+new TimeSpan(9,10,0);
    
    if(centralEuropeResetTime检查过期日期是否小于当前日期,如果是,则添加一天

    DateTime expireDate = new DateTime(2018, 7, 30, 22, 0, 0); //for testing
    
    DateTime tomorrowAt0910 = DateTime.Now.Date.AddHours(9).AddMinutes(10);
    
    if (expireDate.Date < DateTime.Now.Date)
    {
        tomorrowAt0910.AddDays(1);
    }
    
    DateTime expiredatedate=new DateTime(2018,7,30,22,0,0);//用于测试
    DateTime tomorrowAt0910=DateTime.Now.Date.AddHours(9).AddMinutes(10);
    if(expireDate.Date
    检查过期日期是否小于当前日期,如果小于,则添加一天

    DateTime expireDate = new DateTime(2018, 7, 30, 22, 0, 0); //for testing
    
    DateTime tomorrowAt0910 = DateTime.Now.Date.AddHours(9).AddMinutes(10);
    
    if (expireDate.Date < DateTime.Now.Date)
    {
        tomorrowAt0910.AddDays(1);
    }
    
    DateTime expiredatedate=new DateTime(2018,7,30,22,0,0);//用于测试
    DateTime tomorrowAt0910=DateTime.Now.Date.AddHours(9).AddMinutes(10);
    if(expireDate.Date
    检查过期时间是否在9:10之前。如果是这样,不要在localtime中添加一天。快速思考:不要添加一天,而是添加小时数减去本地时区的日期时间偏移量。@VDWWD您能给我举个例子吗?@Filburt好的,我明白了,但您能给我举个例子吗?=)检查过期时间是否在9:10之前。如果是这样,请不要在localtime中添加一天。快速思考:不要添加1天,而是添加小时数减去本地时区的日期时间偏移量。@VDWWD您能给我举个例子吗?@Filburt好的,我明白了,但您能给我举个例子吗?=)返回TimeZoneInfo.ConvertTimeToUtc(centralEuropeResetTime,centralEuropeZone);返回上午7:10而不是上午9:10…我已检查变量centralEuropeResetTime,现在是上午9:10…return语句有问题=(@User987:它返回的是UTC值——实际上是7:10 UTC。没什么问题……如果你的代码需要等到那个时间,UTC值比中欧值有用得多。如果你真的想要本地时间,只需返回
    centralEuropeResetTime
    ——但我认为这不是一个好主意,因为调用方需要ds需要知道时区。好的,知道了=)你能告诉我一些场景吗?在这些场景中,这会更加有用,这样我就可以记住它了?@User987:这意味着除了此方法之外,没有其他代码需要知道重置时间是在中欧时间。请注意,这些信息不能在
    DateTime
    本身中编码,因此你将依赖于调用方读取文档需要知道如何解释
    日期时间
    ,而UTC
    日期时间
    值的
    种类
    UTC
    ,因此您可以判断它是UTC。如果要使用像Noda Time这样的库,您可以返回一个确实知道特定时区的
    ZonedDateTime
    值,因此呼叫者不需要注意。return TimeZoneInfo.ConvertTimeToUtc(centralEuropeResetTime,centralEuropeZone);返回上午7:10而不是上午9:10…我检查了变量centralEuropeResetTime,现在是上午9:10…return语句有问题=(@User987:它返回的是UTC值——实际上是7:10 UTC。没什么问题……如果你的代码需要等到那个时间,UTC值比中欧值有用得多。如果你真的想要本地时间,只需返回
    centralEuropeResetTime
    ——但我认为这不是一个好主意,因为调用方需要ds需要知道时区。好的,知道了=)你能告诉我一些场景吗?在这些场景中,这会更加有用,这样我就可以记住它了?@User987:这意味着除了此方法之外,没有其他代码需要知道重置时间是在中欧时间。请注意,这些信息不能在
    DateTime
    本身中编码,因此你将依赖于调用方读取文档需要知道如何解释
    日期时间
    ,而UTC
    日期时间
    值的
    种类
    UTC
    ,因此您可以判断它是UTC。如果要使用像Noda Time这样的库,您可以返回一个确实知道特定时区的
    ZonedDateTime
    值,因此呼叫者不需要要知道。