Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                    <h1>Native Date Picker (USM Script)</h1>
    <div class="alert alert-info small">
        <p>
            Sample that demonstrates how to display and update dates using the native date Picker
            control both using just a button or using the default date picker intput box.
        </p>
        <p>
            This example uses a <b>DatePickerNative</b> helper class to facilitate binding
            and unbinding of local time values as well as providing the CSS to show a
            button-only date picker.
        </p>
    </div>
    
    <hr>
    
    <h3 class="mt-5">Date Picker Button</h3>
    <hr>
    
    <label class="bold">
        Select a date:
    </label>
    <div>
        <span id="ActiveDate">
        </span>
    
        <button id="DatePickerButton" class="datepicker-native-button btn btn-secondary btn-sm">
            <i class="far fa-calendar-alt"></i>
            <input id="DatePickerButtonInput" type="date" class="datepicker-native-button-input" />
        </button>
    </div>
    
    
    <h3 class="mt-5">Date Picker Input Field</h3>
    <hr>

    <label class="bold" for="DatePickerInput">
        Select a date:
    </label>
    <div>
        <span id="ActiveDateInput">
        </span>

        <input id="DatePickerInput" type="date"
               class="form-control mt-2 date " />
    </div>


    
    <style>
        body {
            padding: 1em;
        }
        .bold {
            font-weight: 600;
        }
        .date {
            width: 10em;
        }
    </style>
              
            
!

CSS

              
                .datepicker-native-button {
    position: relative;
}
.datepicker-native-button-input {
    position: absolute;
    overflow: hidden;
    width: 100%;
    height: 100%;
    right: 0;
    top: 0;
    opacity: 0;
}
.datepicker-native-button-input::-webkit-calendar-picker-indicator {
    position: absolute;
    right: 0;
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    opacity: 0;
    cursor: pointer;
}
              
            
!

JS

              
                var startDate = new Date(2022,10,10);
        console.log(startDate.toLocaleString());
        var el = document.getElementById("DatePickerButtonInput");

        // option configuration syntax        
        let dpn = new  DatePickerNative({            
            element: el,
            activeDate: startDate,
            // + or - 5 days - optional
            min: 5,
            max: 5,
            onDateChanged: function(dt, event) {
                // update date display
                showDate(dt,"ActiveDate");
                console.log(dt.toLocaleString()," Callback date value");

                // element gets a dateValue property
                var el = document.getElementById("DatePickerButtonInput");
                console.log(el.dateValue.toLocaleString()," Date on control");

                // date control value
                //console.log(DateFormatter.formatDate(dpn.options.activeDate,"dw MMM dd, yyyy")," instance.option.activeDate");
            }        
        });

           
         // assign initial display value on page launch
         showDate(startDate,"ActiveDate");
         
        var elInput = document.getElementById("DatePickerInput");

         // parameter syntax - Standard Date Input control
         new DatePickerNative({ 
            element: elInput, 
            activeDate: startDate,                  
            onDateChanged: function(dt, event, instance) {
                showDate(dt, "ActiveDateInput");
                console.log(dt.toLocaleString()," Callback date value Date Input control");
            }        
        });
        showDate(startDate,"ActiveDateInput");


        // display helper
         function showDate(dt,elId) {
            var sdt = dt.toLocaleString();
            document.getElementById(elId).innerText = sdt;
        }                  



// ------------------


function DatePickerNative(el, initialDate, onDateChanged) {
    var _this = this;
    var opt = null;
    
    if (typeof el == "string")
    {
        if (el === "uninitialize") {
            uninitialize();
            return;
        }
        el = document.getElementById(el);
        if (!el) {
            throw new Error("Invalid element provided. Provide either an DOM Element or an id string to an element.");            
        }
    }
    
    if (el.element) {        
        opt = el;  // assume options object was passed
    }
    else {
        opt =  {
            element: el,
            onDateChanged: onDateChanged,
            activeDate: initialDate,
            min: "",  // number as string
            max: ""   // number as string     
        };        
    }
    this.options = opt;

    function intialize(opt) {
        if(typeof opt.activeDate != 'object')
            opt.activeDate = new Date();

        if (opt.element) {
            opt.element.addEventListener("change",datePickerUnbind);     
            datePickerBind(opt.element,opt.activeDate, opt.onDateChanged);
        }
    }    

    function uninitialize(){
        opt.element.removeEventListener("change",datePickerUnbind);        
    }
    
    function datePickerBind(element, dt) {        
        var newDate = localToGmtDate(dt);
        
        opt.element.dateValue = dt;   // original date        
        opt.element.value = newDate;

        if (opt.min)
            opt.element.min = normalizeMin(opt.min);        
        if (opt.max)
            opt.element.max = normalizeMax(opt.max);
    }
    
    function datePickerUnbind(event) {        
        var dt = event.target.valueAsDate;
        let newDate =  utcToLocalDate(dt);
        
        opt.element.dateValue = newDate;
        opt.activeDate = newDate;

        if(opt.onDateChanged){
            opt.onDateChanged(newDate, event, _this);
        }
    }

    function localToGmtDate(localDate) {
        return localDate && new Date(localDate.getTime()-(localDate.getTimezoneOffset()*60*1000)).toISOString().split('T')[0];        
    }
    
    function utcToLocalDate(utcDate) {
        return new Date(utcDate.getTime()+(utcDate.getTimezoneOffset()*60*1000));        
    }

    function normalizeMin(minVal) {    
        if (typeof minVal === "string")
            return minVal;

        if (typeof minVal === "number") {
            let dt = new Date();
            dt = new Date(dt.setDate(dt.getDate() - minVal));
            minVal = dt;
        }

        return localToGmtDate(minVal);
    }

    function normalizeMax(maxVal) {
        if (typeof maxVal === "string") {            
            return maxVal;
        }

        let dt = new Date();
        if (typeof maxVal === "number") {
           dt = new Date();
           dt = new Date(dt.setDate(dt.getDate() + maxVal));
           maxVal = dt;
        }

        return localToGmtDate(maxVal);
    }

    intialize(opt);                
    return _this;
}
              
            
!
999px

Console