Setting format and value in input type=“date”

后端 未结 12 1557
暖寄归人
暖寄归人 2020-12-06 17:05

Is there any way to set the format of ? if no then how can i set date in this field using JavaScript in the default format of

相关标签:
12条回答
  • 2020-12-06 17:06

    Canadian locale happens to follow the same format, too:

    new Date().toLocaleDateString('en-CA')
    
    0 讨论(0)
  • 2020-12-06 17:08

    try this :)

    function getDefaultDate(){
    
        var now = new Date();
        var day = ("0" + now.getDate()).slice(-2);
        var month = ("0" + (now.getMonth() + 1)).slice(-2);
        var today = now.getFullYear()+"-"+(month)+"-"+(day) ;
    
        return today;
    }
    
    $(document).ready(function(){ 
        $("#dateid").val( getDefaultDate()); 
    });
    
    0 讨论(0)
  • 2020-12-06 17:08

    It's ugly, but it works. :/

    var today = new Date().toLocaleString('en-GB').split(' ')[0].split('/').reverse().join('-');
    
    0 讨论(0)
  • 2020-12-06 17:11

    I think this can help

    function myFormatDateFunction(date, format) {
         ...
    }
    
    jQuery('input[type="date"]')                                             
        .each(function(){                                                              
            Object.defineProperty(this,'value',{                                         
              get: function() {                                   
                return myFormatDateFunction(this.valueAsDate, 'dd.mm.yyyy');      
              },                                                                         
              configurable: true,                                                        
              enumerable : true                                                          
            });                                                                          
          });                                                                                
    
    0 讨论(0)
  • 2020-12-06 17:15

    Please check this https://stackoverflow.com/a/9519493/1074944 and try this way also $('input[type="date"]').datepicker().prop('type','text'); check the demo

    0 讨论(0)
  • 2020-12-06 17:16

    I would try jquery and jquery ui to add a nice calendar control that does all the value selecting and formatting for you. simple formatting of an input field isn't enough for demanding users of today's web :)

    Have a look at this link

    0 讨论(0)
提交回复
热议问题