在JavaScript中将本地时间转换为UTC

时间:2015-11-03 09:13:33

标签: javascript

我有这个代码用于处理日期。

<script language="javascript" type="text/javascript">
$("#Reports_SignUpDateAfter_container").datepicker({
    changeYear: true,
    changeMonth: true,
    dateFormat: 'dd-mm-yy', //"dd-mm-yy",
    gotoCurrent: true,
    yearRange: '-10:+10',
    onSelect: function() {
        Reports_SignUpDateAfter_changeDate(false);
    }
});

// react on manual text box editing
$("#Reports_SignUpDateAfter_container").change(function() {
    Reports_SignUpDateAfter_changeDate(true);
});

function Reports_SignUpDateAfter_changeDate(manualChange) {
    var newDate = $("#Reports_SignUpDateAfter_container").datepicker("getDate");

    if (newDate != null) {
        // add time information
        newDate = DateUtils.copyTimeFrom("#Reports_SignUpDateAfter_local", newDate);
        DateUtils.setDate("#Reports_SignUpDateAfter_local", newDate);

        // convert local to UTC
        Reports_SignUpDateAfter_localToUTC();

        if (manualChange) {
            $("#Reports_SignUpDateAfter_container").datepicker("setDate", newDate)
        }
    } else { // empty
        $("#Reports_SignUpDateAfter_local").val("");
        $("#Reports_SignUpDateAfter").val("");
    }
}
// new date as UTC
function Reports_SignUpDateAfter_setDate(newDate) {
    // store time into hidden inputs
    DateUtils.setDate("#Reports_SignUpDateAfter", newDate);

    // set local drop downs and hidden input
    var dateString = DateUtils.getString(newDate);
    dateString = DateUtils.fromUTCToLocal(dateString);

    var localDate = DateUtils.getDateFromString(dateString);

    DateUtils.setDate("#Reports_SignUpDateAfter_local", localDate);
    $("#Reports_SignUpDateAfter_container").datepicker("setDate", localDate);
};

function Reports_SignUpDateAfter_localToUTC() {
    // get string and convert to UTC
    var dateString = $("#Reports_SignUpDateAfter_local").val();

    dateString = DateUtils.fromLocalToUTC(dateString);
    $("#Reports_SignUpDateAfter").val(dateString);
    // conversion end
}

;
</script>

问题是,当我选择2015-11-02开始和结束日期以及结束日期2015-11-04时,它总是将日期转换为UTC(-1:00h),但它并不考虑冬天和夏天的日期变化。该日期应该是相同的(UTC和本地)。有什么线索是这样的吗?

var DateUtils = DateUtils || {
  getDate: function (selector) {
    var value = $(selector).val();

    if (value == null || value == "") {
      var date = new Date();
      date.setHours(0);
      date.setMinutes(0);
      date = DateUtils.cleanSeconds(date);
      return date;
    }

    return DateUtils.getDateFromString(value);
  },

  getClientTimeDifferenceFromUTC: function () {
    var d = new Date()
    return d.getTimezoneOffset();
  },

  setDate: function (selector, date) {

    if (date != null) {
      var old = DateUtils.getDate(selector);

      old.setTime(date.getTime());
      old = DateUtils.cleanSeconds(old);

      $(selector).val(DateUtils.getString(old));

    } else {
      $(selector).val("");
    }
  },

  // copies time from selector and date part from date
  copyTimeFrom: function (selector, date) {
    var value = $(selector).val();
    if (date != null && value != null && value != "") {
      var old = DateUtils.getDate(selector);

      date.setHours(old.getHours());
      date.setMinutes(old.getMinutes());
    }

    return date;
  },

  setHour: function (selector, hour) {
    var old = DateUtils.getDate(selector);
    old.setHours(hour);
    old = DateUtils.cleanSeconds(old);

    $(selector).val(DateUtils.getString(old));
  },

  setMinute: function (selector, minute) {
    var old = DateUtils.getDate(selector);
    old.setMinutes(minute);
    old = DateUtils.cleanSeconds(old);

    $(selector).val(DateUtils.getString(old));
  },

  cleanSeconds: function (date) {
    date.setSeconds(0);
    date.setMilliseconds(0);
    return date;
  },

  // formats date into yyyy-mm-dd hh:mm format
  getString: function (date) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();

    var hours = date.getHours();
    var minutes = date.getMinutes();

    month = ('0' + month).slice(-2);
    day = ('0' + day).slice(-2);
    hours = ('0' + hours).slice(-2);
    minutes = ('0' + minutes).slice(-2);

    return year + "-" + month + "-" + day + " " + hours + ":" + minutes;
  },

  // formats string from yyyy-mm-dd hh:mm into date 
  getDateFromString: function (date) {
    if (date == undefined || date == null) {
      return null;
    }

    var m = date.match(/Date\((\d+)\)/); //handle .NET MVC Date format (i.e. "/Date(1426598616621)/")
    if (m) {
      return new Date(parseInt(m[1]));
    }

    var year = date.substring(0, 4);
    var month = date.substring(5, 7);
    var day = date.substring(8, 10);

    var hours = date.substring(11, 13);
    var minutes = date.substring(14, 16);

    var newDate = new Date(year, month - 1, day, hours, minutes);
    //    newDate.setYear(year);
    //    newDate.setDate(1);
    //    newDate.setMonth(month - 1);
    //    newDate.setDate(day);
    //    newDate.setHours(hours);
    //    newDate.setMinutes(minutes);
    newDate = DateUtils.cleanSeconds(newDate);

    return newDate;
  },

  // dateString in format yyyy-mm-dd hh:mm
  fromLocalToUTC: function (dateString) {
    var date = this.fromLocalToUTCDate(dateString);
    return this.getString(date);
  },

  fromLocalToUTCDate: function (dateString) {
    var dateArray = dateString.substring(0, 10).split("-");
    var timeArray = dateString.substring(11, 19).split(":");
    if (timeArray[0] == null || timeArray[0] == "")
      timeArray[0] = 0;

    if (timeArray[1] == null || timeArray[1] == "")
      timeArray[1] = 0;

    var d = new Date(dateArray[0], dateArray[1] - 1, dateArray[2], timeArray[0], timeArray[1], 0);

    // *****
    // conversion from local date/time to UTC
    var offSet = d.getTimezoneOffset() * 60000;
    var millis = d.getTime() + offSet;
    d.setTime(millis);
    // *****
    return d;
  },

  // dateString in format yyyy-mm-dd hh:mm
  // returns date as string
  fromUTCToLocal: function (dateString) {
    var date = this.fromUTCToLocalDate(dateString);
    return this.getString(date);
  },

  // dateString in format yyyy-mm-dd hh:mm
  // returns date object
  fromUTCToLocalDate: function (dateString, setOffset) {
    var dateArray = dateString.substring(0, 10).split("-");
    var timeArray = dateString.substring(11, 19).split(":");

    if (timeArray[0] == null || timeArray[0] == "")
      timeArray[0] = 0;

    if (timeArray[1] == null || timeArray[1] == "")
      timeArray[1] = 0;

    var d = new Date(dateArray[0], dateArray[1] - 1, dateArray[2], timeArray[0], timeArray[1], 0);

    // *****
    // conversion from UTC to local date/time   
    if (setOffset != false) {
      var offSet = d.getTimezoneOffset() * 60000;
      var millis = d.getTime() - offSet;
      d.setTime(millis);
    }
    // *****

    return d;
  },

0 个答案:

没有答案