Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Xamarin.Forms DateTime.Now() date does not match current date

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading