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

javascript - Display date as dd-mm-yyyy format for input type=date - Stack Overflow

programmeradmin1浏览0评论

I have my code :

<input type="date" class="form-control" id="training_date" 
name="training_date" placeholder=" select" value="" 
onfocus="(this.type='date')" 
onfocusout="(this.type='date')" max=<?php echo date('Y-m-d'); ?>>

I need to display my date in following date-format dd-mm-yyyy format in the textbox.

I have my code :

<input type="date" class="form-control" id="training_date" 
name="training_date" placeholder=" select" value="" 
onfocus="(this.type='date')" 
onfocusout="(this.type='date')" max=<?php echo date('Y-m-d'); ?>>

I need to display my date in following date-format dd-mm-yyyy format in the textbox.

Share Improve this question edited Aug 24, 2017 at 7:00 Om Sao 7,6832 gold badges48 silver badges70 bronze badges asked Aug 24, 2017 at 5:48 HariPriyaHariPriya 1171 gold badge2 silver badges17 bronze badges 9
  • 1 type="date" wont work for firefox and IE – Durga Commented Aug 24, 2017 at 5:50
  • 1 Possible duplicate of Is there any way to change input type="date" format? – BobbyTables Commented Aug 24, 2017 at 5:50
  • jsfiddle/v1pxxsj1 see this – Blue Commented Aug 24, 2017 at 5:52
  • Use jqueryui./datepicker It has support to multiple browsers and you can format date as per your requirement.. – Zedex7 Commented Aug 24, 2017 at 5:52
  • The whole point of type="date" is to display the browsers HTML-5 datepicker (Chrome and Edge only) which displays the date in the users culture, not yours – user3559349 Commented Aug 24, 2017 at 6:01
 |  Show 4 more ments

3 Answers 3

Reset to default 1

What I would remend is to make a array with the 12 months of the year (because the will never change)

_currentDate() {
    var date = new Date();
    for (var i = 0; this.months.length > i; i++) {
      if (date.getMonth() === i) {
        var displayMonth = this.months[i].month;
      }
    }
    var displayDate = date.getDate() + ' ' + displayMonth + ' ' + date.getFullYear();
    return displayDate;
  }

Use the value that you return in your function you just need to insert where you want to display it so in your case your input

Hope this helps :)

Please note: <input type="date"> is not supported in IE and Firefox. Hence, it's not good idea to implement it in as it's against robust UI/UX design, and might invite later bugs. You should use jquery's datepicker, moment.js or bination of both to achieve your requirement.

To close the question and provide what can be done and tested. Here is implementation.

In this example:

  • I am assigning today's date to <input type="date"> by forming a date string in yyyy-mm-dd format and setting attribute value
  • whenever I am changing the the date in #datepicker, I am forming a date string in dd-mm-yyyy format and providing it as value to #textbox

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis./ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
  $(document).ready(function(){
  var d = new Date();      
        
   function twoDigitDate(d){
      return ((d.getDate()).toString().length == 1) ? "0"+(d.getDate()).toString() : (d.getDate()).toString();
    };
        
    function twoDigitMonth(d){
     	return ((d.getMonth()+1).toString().length == 1) ? "0"+(d.getMonth()+1).toString() : (d.getMonth()+1).toString();
    };    
      
    var today_ISO_date = d.getFullYear()+"-"+twoDigitMonth(d)+"-"+twoDigitDate(d); // in yyyy-mm-dd format
        
    document.getElementById('datepicker').setAttribute("value", today_ISO_date);
       
     var dd_mm_yyyy;
     $("#datepicker").change( function(){
       	changedDate = $(this).val(); //in yyyy-mm-dd format obtained from datepicker
        var date = new Date(changedDate);
        dd_mm_yyyy = twoDigitDate(date)+"-"+twoDigitMonth(date)+"-"+date.getFullYear(); // in dd-mm-yyyy format
        $('#textbox').val(dd_mm_yyyy);
        //console.log($(this).val());
        //console.log("Date picker clicked");
        });
        
    });

</script>
</head>
<body>
  <div style="width: 50%;height:50px; float:left;">
  Enter your Birthday!!:<br>
  <input id="datepicker" type="date" name="bday" style="margin-bottom: 200px;"></br><br>
  </div>

  <div style="width: 50%;height:50px; float:right;">
  Date chosen(dd-mm-yyyy):<br>
  <input id="textbox" type="text" value="dd-mm-yyyy"></input>
  </div>
</br></br></br></br></br></br>
<p><strong>Note:</strong> type="date" is not supported in Firefox, or Internet Explorer 11 and earlier versions.</p>

</body>
</html>

You can use the below code to achieve this.

<input type="date" class="form-control" id="training_date" name="training_date" placeholder=" select" value="" onfocus="(this.type='date')" onfocusout="(this.type='date')" pattern="\d{1,2}/\d{1,2}/\d{4}" >

发布评论

评论列表(0)

  1. 暂无评论