最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery datepicker returns NAN as timestamp - Stack Overflow

programmeradmin4浏览0评论

I am using jquery datepicker to select a date and convert to timestamp (epoch time). The script works arbitrarily. It shows timestamp for dates from 1st to 12th of any chosen month but makes time 12:00am of those days (I converted it online). But from 13th to the end of the month it shows NAN as timestamp. Strange enough when the date formated to dd/mm/yy all the days shows correctly.

$(function() {
    $("#datepicker").datepicker({dateFormat: 'dd-mm-yy',
        onSelect: function(dateText, inst) {
              var dtV = $(this).val();
              var d = new Date(dtV);
              var s = parseInt((d)/1000);
              $("#selectedDate").text("on " + dateText + "");
              $(".selectedDate2").text(s.valueOf());
        }
    });
});

I am using jquery datepicker to select a date and convert to timestamp (epoch time). The script works arbitrarily. It shows timestamp for dates from 1st to 12th of any chosen month but makes time 12:00am of those days (I converted it online). But from 13th to the end of the month it shows NAN as timestamp. Strange enough when the date formated to dd/mm/yy all the days shows correctly.

$(function() {
    $("#datepicker").datepicker({dateFormat: 'dd-mm-yy',
        onSelect: function(dateText, inst) {
              var dtV = $(this).val();
              var d = new Date(dtV);
              var s = parseInt((d)/1000);
              $("#selectedDate").text("on " + dateText + "");
              $(".selectedDate2").text(s.valueOf());
        }
    });
});
Share Improve this question edited Feb 25, 2014 at 13:14 gpgekko 3,5963 gold badges34 silver badges35 bronze badges asked Feb 25, 2014 at 13:10 sunny_side_of_lifesunny_side_of_life 1591 gold badge3 silver badges15 bronze badges 1
  • 1 Your case seems like days and months are mixed and the answers provided should solve the problem. However, it might be interesting to mention that in some cases the reason might be Google Translate. – J0ANMM Commented Mar 14, 2019 at 20:06
Add a comment  | 

4 Answers 4

Reset to default 15

I had a similar problem: usually the datepicker worked properly, also for dates with the day after the 12th; but from time to time I saw in Google Analytics that a user was getting NaN-NaN-NaN.

After investigating, I discovered that it could be due to Google Translate.

I checked in my analytics the language of the users getting the error. Almost all of them had a language that my website did not support. That was enough to assume that Google Translator was causing the error.

To solve it, as explained here, add the notranslate class:

$(function() {
    $(".datepicker").datepicker();
    $('.ui-datepicker').addClass('notranslate');
});

When you call new Date()you have to pass a valid date. dd-mm-yy is not a valid date for Dateand it's chenging your month per day.

If you cannot change your date format, try this:

onSelect: function(dateText, inst) {
    var dtV = $(this).val();
    var exploded=dtV.split("-");
    var d = new Date(exploded[2],exploded[1],exploded[0]);

EDIT: Better and shorter, use datepicker's getDate to get a Date object:

onSelect: function(dateText, inst) {
    var d = $(this).datepicker("getDate");

http://jsfiddle.net/9WMvk/

Following works for me. Just added a ui-datepicker class:

$(function () {
        $("#txtStartDate").datepicker();
        $('.ui-datepicker').addClass('notranslate');
    });

It's interpreting your format as if it's mm-dd-yy; JavaScript's native Date class assumes all string input in the format \d\d[-/]\d\d[-/]\d\d is mm-dd-yy.

The yyyy-mm-dd format ISO 8601 is more common and should be interpreted correctly.

发布评论

评论列表(0)

  1. 暂无评论