Reputation: 13
I have a user that keeps getting an error where his DateTime.Now() timestamp is a day or two behind the current system DateTime. This application is working correctly on ~80 other devices.
Restarting the application seems to correct the issue. Does anyone have any experience with this issue?
I am accessing the System.DateTime from a static helper class, here are the variables that get the current date and time:
public static string DateFormat = "yyyy-MM-dd HH':'mm':'ss";
public static string ShortDateFormat = "yyyy-MM-dd HH':'mm";
public static string DateOnlyFormat = "yyyy-MM-dd";
public static string Today = DateTime.Now.ToString(DateFormat);
public static string TodayDate = DateTime.Now.ToString(DateOnlyFormat);
public static string CurrentTime = DateTime.Now.TimeOfDay.ToString();
Upvotes: 0
Views: 1889
Reputation: 17248
Your fields(!)
public static string Today = DateTime.Now.ToString(DateFormat);
are initialized exactly once, when the program starts or the class where this definition is is first used.
You likely want to use a property instead:
public static string Today => DateTime.Now.ToString(DateFormat);
This returns a new string each time it is requested.
Upvotes: 2