How do you preserve a JavaScript date's time zone from browser to server, and back?

前端 未结 4 2065
执笔经年
执笔经年 2020-12-05 01:35

For example, using a date and time control, the user selects a date and time, such that the string representation is the following:

\"6-25-2012 12:00:00 PM\"         


        
相关标签:
4条回答
  • 2020-12-05 01:44

    I am using a filter before sending the date to the server vm.dateFormat = 'yyyy-MM-dd'; dateToSendToServer = $filter('date')(dateFromTheJavaScript, vm.dateFormat);

    0 讨论(0)
  • 2020-12-05 01:46

    Don't rely on JavaScript's Date constructor to parse a string. The behavior and supported formats vary wildly per browser and locale. Here are just some of the default behaviors if you use the Date object directly.

    If you must come from a string, try using a standardized format such as ISO8601. The date you gave in that format would be "2012-06-25T12:00:00". The easiest way to work with these in JavaScript is with moment.js.

    Also, be careful about what you are actually meaning to represent. Right now, you are passing a local date/time, saving a local/date/time, and returning a local date/time. Along the way, the idea of what is "local" could change.

    In many cases, the date/time is intended to represent an exact moment in time. To make that work, you need to convert from the local time entered to UTC on the client. Send UTC to your server, and store it. Later, retrieve UTC and send it back to your client, process it as UTC and convert back to local time. You can do all of this easily with moment.js:

    // I'll assume these are the inputs you have.  Adjust accordingly.
    var dateString = "6-25-2012";
    var timeString = "12:00:00 PM";
    
    // Construct a moment in the default local time zone, using a specific format.
    var m = moment(dateString + " " + timeString, "M-D-YYYY h:mm:ss A");
    
    // Get the value in UTC as an ISO8601 formatted string
    var utc = m.toISOString(); // output: "2012-06-25T19:00:00.000Z"
    

    On the server in .Net:

    var dt = DateTime.Parse("2012-06-25T19:00:00.000Z",   // from the input variable
                            CultureInfo.InvariantCulture, // recommended for ISO
                            DateTimeStyles.RoundtripKind) // honor the Z for UTC kind
    

    Store that in the database. Later retrieve it and send it back:

    // when you pull it from your database, set it to UTC kind
    var dt = DateTime.SpecifyKind((DateTime)reader["yourfield"], DateTimeKind.Utc);
    
    // send it back in ISO format:
    var s = dt.ToString("o"); // "o" is the ISO8601 "round-trip" pattern.
    

    Pass it back to the javascript in moment.js:

    // construct a moment:
    var m = moment("2012-06-25T19:00:00.000Z"); // use the value from the server
    
    // display it in this user's local time zone, in whatever format you want
    var s = m.format("LLL");   // "June 25 2012 12:00 PM"
    
    // or if you need a Date object
    var dt = m.toDate();
    

    See - that was easy, and you didn't need to get into anything fancy with time zones.

    0 讨论(0)
  • 2020-12-05 01:55

    Blame the JSON.Stringfy()... and do:

    x = (your_date);
    x.setHours(x.getHours() - x.getTimezoneOffset() / 60);
    
    0 讨论(0)
  • 2020-12-05 02:07

    Here, I think this is what you are looking for: How to ignore user's time zone and force Date() use specific time zone

    It seems to me that you can do something like this:

    var date = new Date("6-25-2012 12:00:00 PM");
    
    var offset = date.getTimezoneOffset(); // returns offset from GMT in minutes
    
    // to convert the minutes to milliseconds
    offset *= 60000;
    
    // the js primitive value is unix time in milliseconds so this retrieves the 
    // unix time in milliseconds and adds our offset.
    // Now we can put this all back in a date object
    date = new Date(date.valueOf() + offset);
    
    // to get back your sting you can maybe now do something like this:
    var dateString = date.toLocaleString().replace(/\//g,'-').replace(',','');
    
    0 讨论(0)
提交回复
热议问题