Currently I have this html for my calendar
<!--Grid column-->
<div class="col-md-6">
<div class="md-form mb-0">
<input placeholder="Selected date" data-toggle="datepicker" type="text" id="myDate" name="myDate" class="form-control datepicker">
<label for="myDate" id="dateLabel">Estimated Start Date</label>
</div>
</div>
<!--Grid column-->
Currently I have this
var est_start_date = $(this).attr('data-esd');
and the value I place it to the textbox using this code
$('#myDate').val(est_start_date);
which contains value like this
As you can notice the data includes the time also
How can I format the datetime to something like this MM/DD/YYYY
Also separate the time to this format
let date=new Date(document.querySelector('#myDate').value)
Date part ===> date.toLocaleDateString().
Time part ====> date.toLocaleTimeString() for the time part.
from below answer:
You can use the javascript date object to get the required format. Use this,
function formatDate(date) { const arr = [date.getMonth() + 1, date.getDate(), date.getFullYear()] var hours = date.getHours(); var minutes = date.getMinutes(); var ampm = hours >= 12? 'PM': 'AM'; hours = hours % 12; hours = hours? hours: 12; // the hour '0' should be '12' minutes = minutes < 10? '0'+minutes: minutes; var strTime = hours + ':' + minutes + ' ' + ampm; var date = arr.join("/"); return date + " " +strTime; } console.log(formatDate(new Date));
Use this to convert your date to required format,
function formatDate(date) { date = date.split(" "); let dateOne = date[0]; let time = date[1]; time = time.split(":"); time.forEach((val,index)=>{ if(val.length <2){ val = "0" + val; time[index] = val; } }) time = time.join(":"); let str = `${dateOne}T${time}.000Z`; let offset = (new Date()).getTimezoneOffset()*60*1000; date = new Date( (new Date(`${dateOne}T${time}.000Z`)).getTime() + offset); const arr = [date.getMonth() + 1, date.getDate(), date.getFullYear()] var hours = date.getHours(); var minutes = date.getMinutes(); var ampm = hours >= 12? 'PM': 'AM'; hours = hours % 12; hours = hours? hours: 12; // the hour '0' should be '12' minutes = minutes < 10? '0'+minutes: minutes; var strTime = hours + ':' + minutes + ' ' + ampm; var date = arr.join("/"); return date + " " +strTime; } let date = '2019-10-21 1:41:00'; console.log(formatDate(date));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.