#DateTime parsing problem

1 messages · Page 1 of 1 (latest)

wary orchid
#

Hi! In my application, I save DateTime to a text file and then periodically read it. However, when I trying to parse a date saved using standard formatting, the operation fails:

var test = DateTime.UtcNow().ToString(); // 07/27/2025 14:33:00
var isParsed = DateTime.TryParse(test, out var date); // isParsed = false 

I tried to find bug in the nanoFramework source code in order to make a PR with corrections, but apparently the required functionality is implemented natively in C++, and my knowledge of this language is not enough to create PR and support it until the merge.

Well, I looked in the C++ source code to see what date formats it can recognize, and now I will convert my date to one of these formats, but I would really appreciate if someone could fix this behavior in the future. Anyway, I thought I'd let you know that this problem exists.

P.S. Device: ESP32 WROOM (rev 3), latest firmware.

trail flame
#

hey @wary orchid is not ISO format ... looks like American date..
But it's in the comment you wrote, not the real output of ToString()
Having said that, IIRC there are several Unit Test in mscorlib that use the same pattern that you have there, so I'm surprise that is not correct...

#

is that ToString() called in your nanoFramework application? if not, you have to specify a format supported by nanoFramework

wary orchid
# trail flame is that `ToString()` called in your nanoFramework application? if not, you have ...

Yes, this is the date received from the nanoFramework application. I get date from my RTC module(PCF8563), and save it to a file like this:

public static void WriteLastFeeding(DateTime newValue)
            => File.WriteAllText(LastFeedingFile, newValue.ToString());

Then, in another place in the program, I read from this file and convert it to a date:

public static DateTime ReadLastFeeding()
        {
            if (!File.Exists(LastFeedingFile)) return DateTime.MinValue;

            var lastFeedingText = File.ReadAllText(LastFeedingFile);
            Debug.WriteLine(lastFeedingText);

            var isParsed = DateTime.TryParse(lastFeedingText, out var date);

            if (isParsed) return date;

            return DateTime.MinValue;
        }
trail flame
#

Odd... If you output newValue with debug.writeline what do you see?

And if you call the DateTime.TryParse with a ISO format string, does it work?