junkone
junkone

Reputation: 1465

why does linqpad serialization ignore the midnight in visual representation

I have this hourly data. it shows up properly formatted for all the rows except the ones with midnight

code is uploaded at https://share.linqpad.net/429vqowu.linq

screenshot look at the midnight and notice how it does not show the 00:00:00

void Main()
{
    // Generate hourly DateTime data for 2 days
    var startDate = new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc); // Example start date
    var endDate = startDate.AddDays(2);
    var hourlyDates = new List<DateTime>();

    for (var date = startDate; date <= endDate; date = date.AddHours(1))
    {
        hourlyDates.Add(date);
    }

    // Serialize the list to JSON
    var json = Newtonsoft.Json.JsonConvert.SerializeObject(hourlyDates);

    // Deserialize the JSON back to a list of DateTime objects
    var deserializedDates = Newtonsoft.Json.JsonConvert.DeserializeObject<List<DateTime>>(json);

    // Dump the deserialized data to LinqPad
    deserializedDates.Dump();
}

// You can define other methods, fields, classes and namespaces here

Upvotes: 0

Views: 65

Answers (1)

sgmoore
sgmoore

Reputation: 16077

If you don't like this behaviour then with LINQPad 8.4 and later you can set your own custom format, for example

DumpOptions.Default.FormatStrings.DateTime = "yyyy-MM-dd hh:mm:ss tt";

This isn't actually related to serialization as the same thing happens if you dump your source hourlyDates and only happens if you dump to DataGrids rather than the default Rich Text(Html).

What I think is happening is that LinqPad is simply detecting whether the value has a time component and using a different format in that case.

Upvotes: 1

Related Questions