Click here to Skip to main content
15,914,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I convert millisecond to date time in jquery. On change it works but on load it does not work.

What I have tried:

JavaScript
var dateFormat = "M/D/YYYY, h:mm A";

$('.datepicker').each(function(i, e) {
        new Pikaday({
           field: e,
           format: dateFormat,
           disableDayFn: function (date) { 
               return moment(date).isAfter(new Date());
           },
           onOpen: function() {
               if (!this._o.field.value) {
                   this.setDate(new Date());
               }
           }
        });
        $(e).on('change', function(ev) {
            onDatePickerSelect(ev);
        });
    }); 

function setDateOnLoad() {
    $("#dateFromTimestamp").each(function(i, e) { 
        new Pikaday({
           field: e,
           format: dateFormat,
           value: new Date(1488484260000)                        
        });

    }); 
}
Posted
Updated 21-Mar-17 5:52am
v2

1 solution

When the script runs, the document probably hasn't been drawn yet.

Try this:
JavaScript
var dateFormat = "M/D/YYYY, h:mm A";

$( document ).ready(()=>{ // or $( document ).ready(function(){
    $('.datepicker').each(function(i, e) {
        new Pikaday({
           field: e,
           format: dateFormat,
           disableDayFn: function (date) { 
               return moment(date).isAfter(new Date());
           },
           onOpen: function() {
               if (!this._o.field.value) {
                   this.setDate(new Date());
               }
           }
        });
        $(e).on('change', function(ev) {
            onDatePickerSelect(ev);
        });
    }); 
});

function setDateOnLoad() {
    $("#dateFromTimestamp").each(function(i, e) { 
        new Pikaday({
           field: e,
           format: dateFormat,
           value: new Date(1488484260000)                        
        });

    }); 
}


this will ensure that the code runs only after the doc is drawn
 
Share this answer
 
Comments
ZurdoDev 21-Mar-17 12:15pm    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900