0
votes

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

2
The now variable can't possibly be null - it's a non-nullable value type. This sounds like a debugger issue. - Jon Skeet
I also don't understand why retrieving time parts of a DateTime object don't work, I think it is because the Local time has PM and AM in local language that I configured my computer. - mz1378
"Don't work" is pretty vague - we don't know how you're examining these values at all, and that seems to be entirely separate (and incompatible) with the value being null. I suggest you change your question to add some diagnostics that display now.Hour, now.Minute, now.DayOfYear etc, along with sample output (and what the current time is when you run it). (But no, DateTime.Hour should not be affected by formatting.) - Jon Skeet
Note that an hour of 12 is usually regarded as PM. There are 12 hours of AM (0-11) and 12 hours of PM (12-23). - Jon Skeet

2 Answers

1
votes

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

-1
votes

Solved my problem using this code, Maybe local Am Pm is the problem:

DateTime now = DateTime.Now.Date; // here
    int hour = DateTime.Now.Hour; // here       
    double noon = 0;
    int dayOfYear = now.DayOfYear; // here
    bool isAM = false;
    if (dayOfYear >= 80 && dayOfYear <= 260)
    {
        noon = 13;
        int h = hour; 
        isAM = hour >= 13 ? false : true; // here
    }