【问题标题】:Rounding up time and dates C# - Issue with increasing date by 1 when time = 00:00:00四舍五入时间和日期 C# - 当时间 = 00:00:00 时日期增加 1 的问题
【发布时间】:2014-07-11 13:42:47
【问题描述】:

好的,基本上我正在创建一个重新写入文件的文本文件格式化程序,我正在使用正则表达式来获取日期和时间值,现在我将它们四舍五入,然后当数据到达午夜时,日期增加一 -到目前为止一切正常,包括日期增加,除了它使用今天的日期作为开始日期而不是我文件中的日期。代码很长,但在这里...

    line = Regex.Replace(line, @"\s+", ",");
       string[] split = date1.Split(' ');


       string inputime= split[0];

        DateTime dt ;
        System.Globalization.CultureInfo enUS = new System.Globalization.CultureInfo("en-US");
        DateTime.TryParseExact(inputime, "HH:mm:ss", enUS, System.Globalization.DateTimeStyles.None, out dt);
          DateTime rounded;
          if (dt.Minute >= 30)
          {
           rounded = Round(dt, "up");
          }
           else
           {
           rounded = Round(dt, "down");
                                }


                  writer.WriteLine( rounded.ToString("dd/MM/yyyy") + "," + rounded.ToString("HH:mm:ss") + "," + line);
                                  count1--;

                 line = Regex.Replace(line, @"\s+", ",");

                            }
                        }
                    }
                    writer.Close();
                }

                MessageBox.Show("Formatting Complete");
            }
        }

        public static DateTime Round(DateTime dateTime, string direction)
        {
            var updated = dateTime.AddHours(1);


            var updated1 = dateTime.AddDays(1);


            switch (direction)
            {
                case "up":
                    if (dateTime.Hour == 00)
                    {
                        updated1.AddDays(dateTime.Day);

                        return new DateTime(updated1.Year, updated1.Month, updated1.Day, updated.Hour, 0, 0, dateTime.Kind);
                    }
                    else
                    {
                        return new DateTime(updated.Year, updated.Month, updated.Day, updated.Hour, 0, 0, dateTime.Kind);
                    }
                case "down":
                    {
                        updated.AddHours(dateTime.Hour);
                        return new DateTime(updated.Year, updated.Month, updated.Day, updated.Hour, 0, 0, dateTime.Kind);
                    }
            }
            return (dateTime);
        }

【问题讨论】:

  • 如果您调用 AddHours(1) 并且结果通过午夜,则该天自动增加 1,如果您通过一个月的最后一天,该月将自动增加 1(您可以想象会发生什么如果月份是 12,则年份)

标签: c# date time logic


【解决方案1】:

您的代码确实应该在其他方面更正,但对于您的问题:

在这条线上

DateTime.TryParseExact(inputime, "HH:mm:ss", enUS, System.Globalization.DateTimeStyles.None, out dt);

您正在创建一个DateTime,其中包含小时、分钟和秒。

如果您不提供年、月、日,则将采用当前日期。

因此,您还需要解析年、月、日来获取这些值。

【讨论】:

    【解决方案2】:

    将时间读入TimeSpan(使用例如TimeSpan.Parse),并使用文件的日期作为起始值。要获得完整的日期和时间,您可以简单地做

    currentDate + currentTime
    

    另外,午夜要小心。 IIRC 某些文化将午夜算作前一天的一部分,而其他文化可能会将其算作第二天的一部分。

    【讨论】:

      猜你喜欢
      • 2021-07-16
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      • 2014-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多