You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中DateTime.Now输出时间不更新问题求助

问题:DateTime生成的TIMESTAMP字符串始终不更新

我写了一个用到DateTime.Now的简单时间程序,它是我一个更大程序的一部分,但发现个小问题:生成的TIMESTAMP字符串用来记录操作时间并输出到控制台,但它始终不更新。我确定之前是正常的,现在不行了,是不是漏了什么?另外,我本来不需要秒数,只是测试加上的,试过DateTime.UtcNow也一样。下面是我的代码和输出:

代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1TimeStampNEW
{
    internal class Program
    {
        private static DateTime Now = DateTime.Now;

        static string strDate = Now.ToString("MM/dd/yyyy");
        static string timeString = DateTime.Now.ToString("hh:mm ss tt");
        static string TIMESTAMP = strDate + " " + timeString + " ";

        static void Main(string[] args)
        {
            char input;

            do
            {
                Console.WriteLine("\n"+TIMESTAMP);
                Console.WriteLine("\nWould you like to repeat? Y/N");

                input = Convert.ToChar(Console.ReadLine());
            }
            while (input == 'y');

            Console.WriteLine("\nPress any key to exit.");
            Console.ReadKey();
        }
    }
}

输出

08/30/2022 12:57 58 PM

Would you like to repeat? Y/N
y

08/30/2022 12:57 58 PM

Would you like to repeat? Y/N

解决方案

问题出在静态字段的初始化时机:你定义的TIMESTAMP是静态字段,它的赋值逻辑只在类第一次加载初始化的时候执行一次,之后循环里输出的都是第一次生成的时间,自然不会更新。

解决方法一:每次循环时重新生成TIMESTAMP

把时间字符串的生成逻辑放到循环内部,这样每次循环都会获取当前最新时间:

static void Main(string[] args)
{
    char input;

    do
    {
        // 每次循环重新生成当前时间的时间戳
        string strDate = DateTime.Now.ToString("MM/dd/yyyy");
        // 如果不需要秒数,把格式改成 "hh:mm tt" 即可
        string timeString = DateTime.Now.ToString("hh:mm ss tt");
        string TIMESTAMP = strDate + " " + timeString + " ";
        
        Console.WriteLine("\n" + TIMESTAMP);
        Console.WriteLine("\n是否重复?是/否");

        // 加上ToLower()避免大小写判断问题
        input = Convert.ToChar(Console.ReadLine().ToLower());
    }
    while (input == 'y');

    Console.WriteLine("\n按任意键退出。");
    Console.ReadKey();
}

解决方法二:将TIMESTAMP改为静态属性

通过静态属性的getter方法,每次访问时自动重新计算当前时间,这样无需修改循环逻辑:

namespace ConsoleApp1TimeStampNEW
{
    internal class Program
    {
        // 静态属性,每次访问时生成最新时间戳
        private static string TIMESTAMP
        {
            get
            {
                string strDate = DateTime.Now.ToString("MM/dd/yyyy");
                // 不需要秒数就改成 "hh:mm tt"
                string timeString = DateTime.Now.ToString("hh:mm ss tt");
                return strDate + " " + timeString + " ";
            }
        }

        static void Main(string[] args)
        {
            char input;

            do
            {
                Console.WriteLine("\n" + TIMESTAMP);
                Console.WriteLine("\n是否重复?是/否");

                input = Convert.ToChar(Console.ReadLine().ToLower());
            }
            while (input == 'y');

            Console.WriteLine("\n按任意键退出。");
            Console.ReadKey();
        }
    }
}

不管用DateTime.Now还是DateTime.UtcNow,只要确保每次获取的是当前实时时间,就能解决不更新的问题。

内容的提问来源于stack exchange,提问作者MidnightBlueKnight

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 15:19:21