My Javascript works but it shows time format as 2019-04-11T14:00:00 and needs to be in 4/11/2019 1:00:00 PM format. The time is displayed on the txtStartTime listed below.What's a good way to do that?
<script type="text/javascript">
function load() {
var xhttp = new XMLHttpRequest();
xhttp.open('post', 'instructorcourse.aspx/GetCourseTitles', true);
xhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhttp.setRequestHeader('Content-Type', 'application/json');
xhttp.onreadystatechange = function () {
if (xhttp.readyState == XMLHttpRequest.DONE && xhttp.status == 200) {
var $titlesDropDown = document.getElementById('DropDownList1'),
courses = JSON.parse(JSON.parse(this.responseText).d),
html = '';
//courses.forEach(function (course) {
// var value = course.RecordID + '|' + course.CourseCode + '|' + course.Title;
courses.forEach(function (course) {
var value = course.RecordID + '|' + course.CourseCode + '|' + course.Title + '|' + course.StartTime;
html += '<option value="' + value + '">' + course.Title + '</option>';
});
$titlesDropDown.innerHTML = html;
}
};
xhttp.send(JSON.stringify({ instructorId: '<%=txtInstructorID.Text%>' }));
}
document.addEventListener('DOMContentLoaded', load);
My Javascript works but it shows time format as 2019-04-11T14:00:00 and needs to be in 4/11/2019 1:00:00 PM format. The time is displayed on the txtStartTime listed below.What's a good way to do that?
You misunderstand what happening. The date format was manually generated on the server.
Convert the date string to a Date type in the JavaScript application. From there you can use the JavaScript Date object to generate date and time strings as shown below.
var d = new Date(startTime);
document.getElementById('<%=nameof(txtStartTime)%>').value = d.toLocaleDateString() + ' ' + d.toLocaleTimeString();
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
If I try this line I get the error in the Internet Explorer debugger Object doesn't support property or method 'toLocaleDateString'. Is there a way to write that function on one line?
document.getElementById('txtStartTime').value = Date(startTime).toLocaleDateString() + ' ' + Date(startTime).toLocaleTimeString();
It works in one line if I try this document.getElementById('<%=nameof(txtStartTime)%>').value = new Date(startTime).toLocaleDateString() + ' ' + new Date(startTime).toLocaleTimeString();
Member
18 Points
50 Posts
Javascript shows time format as 2019-04-11T14:00:00 and needs to be in 4/11/2019 1:00:00 PM forma...
Apr 12, 2019 07:44 PM|Tom4IT|LINK
My Javascript works but it shows time format as 2019-04-11T14:00:00 and needs to be in 4/11/2019 1:00:00 PM format. The time is displayed on the txtStartTime listed below. What's a good way to do that?
<script type="text/javascript">
function load() {
var xhttp = new XMLHttpRequest();
xhttp.open('post', 'instructorcourse.aspx/GetCourseTitles', true);
xhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhttp.setRequestHeader('Content-Type', 'application/json');
xhttp.onreadystatechange = function () {
if (xhttp.readyState == XMLHttpRequest.DONE && xhttp.status == 200) {
var $titlesDropDown = document.getElementById('DropDownList1'),
courses = JSON.parse(JSON.parse(this.responseText).d),
html = '';
//courses.forEach(function (course) {
// var value = course.RecordID + '|' + course.CourseCode + '|' + course.Title;
courses.forEach(function (course) {
var value = course.RecordID + '|' + course.CourseCode + '|' + course.Title + '|' + course.StartTime;
html += '<option value="' + value + '">' + course.Title + '</option>';
});
$titlesDropDown.innerHTML = html;
}
};
xhttp.send(JSON.stringify({ instructorId: '<%=txtInstructorID.Text%>' }));
}
document.addEventListener('DOMContentLoaded', load);
function changeCourse(e) {
//var temp = e.target.value.split('|'),
// //recordID = parseInt(temp[0]),
// recordID = temp[0],
// courseCode = temp[1],
// title = temp[2];
var temp = e.target.value.split('|'),
//recordID = parseInt(temp[0]),
recordID = temp[0],
courseCode = temp[1],
title = temp[2],
startTime = temp[3];
<%--document.getElementById('<%=nameof(txtInstructorID)%>').value = recordID;--%>
document.getElementById('<%=nameof(txtRecordID)%>').value = recordID;
document.getElementById('<%=nameof(txtCourseCode)%>').value = courseCode;
document.getElementById('<%=nameof(txtCourseTitle)%>').value = title;
document.getElementById('<%=nameof(txtStartTime)%>').value = startTime;
}
</script>
All-Star
43831 Points
18762 Posts
Re: Javascript shows time format as 2019-04-11T14:00:00 and needs to be in 4/11/2019 1:00:00 PM f...
Apr 12, 2019 10:15 PM|mgebhard|LINK
You misunderstand what happening. The date format was manually generated on the server.
Convert the date string to a Date type in the JavaScript application. From there you can use the JavaScript Date object to generate date and time strings as shown below.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
FYI, the code indicates that you are serializing and deserializing twice. Generally, you don't want to do that.
Contributor
2100 Points
705 Posts
Re: Javascript shows time format as 2019-04-11T14:00:00 and needs to be in 4/11/2019 1:00:00 PM f...
Apr 15, 2019 06:50 AM|Jenifer Jiang|LINK
Hi Tom4IT,
According to your description and code, I suggest that you could use moment.js to convert your date format.
I've made a test and maybe you could refer to.
result:
Reference:http://momentjs.com/docs/#/manipulating/local/
Best Regards,
Jenifer
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
18 Points
50 Posts
Re: Javascript shows time format as 2019-04-11T14:00:00 and needs to be in 4/11/2019 1:00:00 PM f...
Apr 16, 2019 01:47 PM|Tom4IT|LINK
If I try this line I get the error in the Internet Explorer debugger Object doesn't support property or method 'toLocaleDateString'. Is there a way to write that function on one line?
document.getElementById('txtStartTime').value = Date(startTime).toLocaleDateString() + ' ' + Date(startTime).toLocaleTimeString();
It works in one line if I try this
document.getElementById('<%=nameof(txtStartTime)%>').value = new Date(startTime).toLocaleDateString() + ' ' + new Date(startTime).toLocaleTimeString();