How to convert date string into date format in js


How to convert date string into date format in js



Note: START_DATE & END_DATE in String Format, String Format is :"29-06-2018 14:11"


function checkConstraints2() {
debugger
var currentyear = document.getElementById('START_DATE').value;
var builtYear = document.getElementById('END_DATE').value;
var dif = currentyear < builtYear
if (dif) {
document.getElementById("validationBuiltyaer2").style.display = "none";
return true;
}
else {
document.getElementById("validationBuiltyaer2").style.display = "block";
document.getElementById("validationBuiltyaer2").innerText = "End Date Should be Greater Than From Date";
flag = false;
}
}





you can use momentjs
– Vivek
Jun 29 at 8:58


momentjs





we want to check this condition currentyear < builtYear. It is string so its only comparing day, its not comparing month & year. so we want to convert its in Date Format.
– Anji Tandel
Jun 29 at 9:08






then you can use only year by "YYYY" to parse date
– Vivek
Jun 29 at 9:10





Why is this question tagged C# and Oracle? If the immediate question is purely JavaScript then please remove those tags.
– MT0
Jun 29 at 9:26





2 Answers
2



Here's how you can use moment to parse date string -


moment




var dateString = "29-06-2018 14:11";

console.log(moment(dateString, "DD-MM-YYYY HH:mm"));

var startDateString = "29-06-2018 14:11";
var endDateString = "30-06-2018 14:11";

var startDate = moment(startDateString, "DD-MM-YYYY HH:mm");
var endDate = moment(endDateString, "DD-MM-YYYY HH:mm");

console.log(startDate < endDate);


https://momentjs.com/downloads/moment.min.js





Thank you so much @Vivek
– Anji Tandel
Jun 29 at 9:34



Try this:


var parts = '29-06-2018 14:11'.split(' ')[0].split('-');
// Please substract 1 from month part as javaScript counts months from 0:
// January - 0, February - 1, etc.
var mydate = new Date(parts[2], parts[1] - 1, parts[0]);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift