简体   繁体   English

Javascript yyyy-mm-dd转换为错误的日期(mm / dd / yyyy)

[英]Javascript yyyy-mm-dd converting to incorrect date (mm/dd/yyyy)

Here is the code where the user enters a date: (it has to be a date picker on my end, but the form has to submit the date field as text – don't ask) 这是用户输入日期的代码:(它必须是我最后的日期选择器,但表单必须提交日期字段作为文本 - 不要问)

On submit, I call validation logic in javascript. 提交时,我在javascript中调用验证逻辑。 I've attached a screenshot of what it looks like when I try to enter 01/01/2001 as the users birthday. 我附上了当我尝试以01/01/2001作为用户生日输入时的截图。 It looks like when I'm converting the value string to a Date object, it's converting to the wrong date and time. 看起来当我将值字符串转换为Date对象时,它转换为错误的日期和时间。 If it would just convert correctly, I could adjust the month and day and year and build a string to send in my second object. 如果它只是正确转换,我可以调整月,日和年,并构建一个字符串以发送到我的第二个对象。

Attaching the picture… 附上图片......

I've messed around with UTC and timezones, but to no avail. 我已经弄乱了UTC和时区,但无济于事。

I need my output to be a text string "01/01/2001" which I can build as long as I have the correct date going in..but it seems to calculate wrong no matter what I try. 我需要我的输出是一个文本字符串“01/01/2001”我可以构建,只要我有正确的日期进入..但它似乎计算错误无论我尝试什么。

When you construct the Date it is assumed that the string represents a time in UTC since no timezone was provided. 构造Date时,假定字符串表示UTC中的时间,因为没有提供时区。 The string is parsed as UTC, but the Date object uses your browser's local timezone. 该字符串被解析为UTC,但Date对象使用您的浏览器的本地时区。

One way to fix that is to use getUTCDay instead of getDay . 解决这个问题的一种方法是使用getUTCDay而不是getDay Same applies to month and year . 同样适用于月份年份

Using a jquery datapicker library did the trick. 使用jquery数据贴图库就可以了。

function initDatePickers() {
jQuery('.om-datepicker-trigger').click(function () {
    var defaultDatePickerOptions = {
        showOtherMonths: true,
        changeMonth: true,
        changeYear: true,
        defaultDate: '-45y',
        dateFormat: 'mm/dd/yy',
        beforeShow: function (input, inst) {
            var widget = jQuery(inst).datepicker('widget');
            widget.css('margin-left', jQuery(input).outerWidth() + 3 - 
widget.outerWidth());
        },
        //buttonImage: "/img/button_calendar.png",
        //buttonImageOnly: true,
        showOn: "both"
    };

    var $input = jQuery(this).parent().find('.om-input-date').first();

    if ($input.hasClass('om-min-date-today')) {
        var minDateTodayOptions = defaultDatePickerOptions;
        minDateTodayOptions.defaultDate = 0;
        minDateTodayOptions.minDate = 0;
        $input.datepicker(minDateTodayOptions);
        $input.datepicker('show');
    } else {
        $input.datepicker(defaultDatePickerOptions);
        $input.datepicker('show');
    }
});

jQuery('.om-input-date').click(function () {
    jQuery(this).next('.om-datepicker-trigger').trigger('click');
});


// Datepicker
// --------------------------------------------------------
jQuery('.om-input-date').keyup(function () {
    var inputDOBBox = jQuery(this);
    var dateValue = inputDOBBox.attr('value');
    if (dateValue.length == 3 || dateValue.length == 6) {
        var first = dateValue.substring(0, dateValue.length - 1);
        var last = dateValue.substring(dateValue.length - 1);
        if (last != "/" && last != "-") {
            inputDOBBox.attr('value', first + "/" + last);
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM