Bootstrap Date Time Control

Source: Internet
Author: User
Tags datetime getdate locale min
datetime Control

Bootstrap a DateTime control, which is very simple to use.

First, add a reference to the date-time control

@*datetime control *@
    <link href= "~/content/bootstrap/css/bootstrap-datetimepicker.min.css" rel= "stylesheet"/ >
    <script src= "~/content/bootstrap/js/moment-with-locales.js" ></script>
    <script src= "~ /content/bootstrap/js/bootstrap-datetimepicker.min.js "></script>
    <script src=" ~/content/ Bootstrap/js/bootstrap-datetimepicker.zh-cn.js "></script>

Link: http://pan.baidu.com/s/1sl56aw1 Password: jc2y
Page code

<a class= ' input-group date ' id= ' dateTimePicker1 ' style= ' float:left; left:320px; " >
                <input type= ' text ' class= "Form-control" id= ' nowdate ' style= "width:150px; height:30px;"/>
                < Span class= "Input-group-addon" style= "float:left; width:50px; height:30px; " >
                    <span class= "Glyphicon glyphicon-calendar" ></span>
                </span>
            </a>

JavaScript code

the DateTime control is set to function as a date () {
    $ (' #datetimepicker1 '). DateTimePicker ({
        language: ' ZH-CN ',//display Chinese
        Format: ' Yyyy-mm-dd ',//display format
        minview: ' Month ',//settings only display to month
        initialdate:new Date (),
        autoclose:true,// Check Auto close
        todaybtn:true,//Show Today button
        Locale:moment.locale (' ZH-CN ')
    });
    The default gets the current date
    var today = new Date ();
    var nowdate = (Today.getfullyear ()) + "-" + (Today.getmonth () + 1) + "-" + today.getdate ();
    Processing of date formats
    var date = new Date (nowdate);
    var mon = date.getmonth () + 1;
    var day = Date.getdate ();
    var mydate = date.getfullyear () + "-" + (Mon < 10?) "0" + Mon:mon) + "-" + (Day < 10?) "0" + day:day);
    document.getElementById ("Nowdate"). Value = MyDate;
}

The setting of the DateTime control's default value, it should be noted that the ID used in JS is the ID of the input tag.

document.getElementById ("Nowdate"). Value = MyDate;

In addition, setting the default date has a format problem, the page loads after the date time, the month and the number of days is 1~9, it is not in front of 0.

However, the date time after the date time control is selected, it is preceded by 0.

Workaround, first get the current datetime, and then use the format to process it.

    The default gets the current date
    var today = new Date ();
    var nowdate = (Today.getfullyear ()) + "-" + (Today.getmonth () + 1) + "-" + today.getdate ();
    Processing of date formats
    var date = new Date (nowdate);
    var mon = date.getmonth () + 1;
    var day = Date.getdate ();
    var mydate = date.getfullyear () + "-" + (Mon < 10?) "0" + Mon:mon) + "-" + (Day < 10?) "0" + day:day);
    document.getElementById ("Nowdate"). Value = MyDate;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.