简体   繁体   English

DateTime.Now 不更新 C#

[英]DateTime.Now Not Updating C#

I have this simple time program that uses DateTime.Now , it part of a bigger program I have but this is just a small error I notice.我有一个使用DateTime.Now的简单时间程序,它是我拥有的更大程序的一部分,但这只是我注意到的一个小错误。 I made a TimeStamp string to get the time of whenever something was done in the code and sending to the console.我创建了一个 TimeStamp 字符串来获取在代码中完成某些操作并发送到控制台的时间。 However, its not updating?但是,它没有更新吗? I swear at one point it did work and was updating but then it stopped, am I missing something?我发誓它确实有效并且正在更新,但后来它停止了,我错过了什么吗?

ps I dont need the seconds but I put it there for testing purposes, I have also tried it with DateTime.UtcNow and it still didn't work ps 我不需要秒,但我把它放在那里是为了测试目的,我也用DateTime.UtcNow尝试过它仍然没有用

Code代码

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();


        }

    }
}

Output Output

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 is just a string. TIMESTAMP只是一个字符串。 Once you set it, there is no relationship to the DateTime type.设置后,与DateTime类型没有任何关系。

You want something more like this:你想要更像这样的东西:

do
{
    Console.WriteLine($"\n{DateTime.Now:MM/dd/yyyy hh:mm:ss tt}");
    Console.WriteLine("\nWould you like to repeat? Y/N");

    input = Console.ReadLine()[0];
}
while (input == 'y');

If you want a string value formatted a certain way every time you use it, one way is via a property :如果您希望每次使用时都以某种方式格式化字符串值,一种方法是通过属性

public string TIMESTAMP {get { return DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt"); } }

Of course, this needs to be in a class somewhere, but then you could use it like this, which looks more like the original code:当然,这需要在某处的 class 中,但是你可以像这样使用它,它看起来更像原始代码:

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

    input = Console.ReadLine()[0];
}
while (input == 'y');

And of course, a method can do the same thing, but for some reason this just feels like a property fits more what you are trying to do.当然,一个方法可以做同样的事情,但由于某种原因,这感觉就像一个属性更适合你想要做的事情。

Try something like this尝试这样的事情

public string TIMESTAMP {get {return DateTime.Now.ToString();} };

Every time you use TIMESTAMP it will get the current date and time and return it as a string.每次使用 TIMESTAMP 时,它都会获取当前日期和时间并将其作为字符串返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM