Search code examples
javascriptjqueryasp.netvb.netutc

Working With UTC Using jQuery


In an ASP.Net web form, I am converting the current date and time to UTC format.

Dim ClickDT As String = DateTime.Now.ToFileTimeUtc.ToString

This value generated in VB.Net will be passed as a URL parameter to another web page that will process it using jQuery.

On the jQuery side, I want to have a date time range that give the user a 10 minute window. This is what I have been using, but how do I modify it so that it takes into account the UTC value that is passed?

var DateSent = decodeURIComponent(GetURLParameter('MyUTCDateValue'));
var PassedDT = new Date(DateSent);
var MinCurrentDT = new Date($.now());
var MaxCurrentDT = new Date($.now());

MinCurrentDT.setMinutes(MinCurrentDT.getMinutes() - 5);
MaxCurrentDT.setMinutes(MaxCurrentDT.getMinutes() + 5);

 if (PassedDT >= MinCurrentDT && PassedDT <= MaxCurrentDT) {
        alert('Date Time is good.');                         
    }
    else {
        alert('Date is outside range.');
    }

Solution

  • Add the Local Time Zone's Offset to the MinCurrentDT and MinCurrentDT variables.

    //Get offset in minutes:
    var LocalOffset = new Date().getTimezoneOffset();
    
    MinCurrentDT.setMinutes(MinCurrentDT.getMinutes() - 5 + LocalOffset);
    MaxCurrentDT.setMinutes(MaxCurrentDT.getMinutes() + 5 + LocalOffset);