Search code examples
c#blazor-client-side

DateTime.Now in Blazor Webassembly is null


I have this code:

 DateTime now = DateTime.Now; // null
    decimal noon = 0m;
    int dayOfYear = now.DayOfYear; // reports 98, correct
    bool isAM = false;
    if (dayOfYear >= 80 && dayOfYear <= 260)
    {
        noon = 13m;
        int h = now.Hour; // 0 (should be 15)          
        isAM = now.Hour >= 13 ? false : true; // true (should be false)
    }

in Blazor webassembly in a component, When I debug, While 'now' variable is null the 'dayOfYear' variable correctly shows 98, But 'now.Hour' is 0, And the correct computer time is 3:30 PM, Why 'now' variable is null? And how it correctly reports dayOfYear 98? why Hour is 0!? I am using Blazor 3.2 using its new debugging capability.

Update: The hour is correct, Only the DateTime object's 'null' value put me in doubt as the following screenshot shows:

enter image description here


Solution

  • I ran into a similar problem the other day. At this time Blazor Wasm doesn't contain any time zone info and local time zone is always UTC. However, there is a fix available in order to get the right time zone info. Check this out: Blazor WebAssembly App (client-side) Time Zone Kit