How to convert date in different format in javascript?
File name : index.php
function dateFormat(inputDate, format) {
//parse the input date
const date = new Date(inputDate);
//extract the parts of the date
const day = date.getDate();
const month = date.getMonth() + 1;
const year = date.getFullYear();
//replace the month
format = format.replace("MM", month.toString().padStart(2,"0"));
//replace the year
if (format.indexOf("yyyy") > -1) {
format = format.replace("yyyy", year.toString());
} else if (format.indexOf("yy") > -1) {
format = format.replace("yy", year.toString().substr(2,2));
}
//replace the day
format = format.replace("dd", day.toString().padStart(2,"0"));
$( document ).ready(function() {
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = yyyy + '-' + mm + '-' + dd;
today1 = dd + '-' + mm + '-' + yyyy;
var fmdate= today
var month = parseInt(fmdate.substr(5,2));
var day = fmdate.substr(8,2)
var year = fmdate.substr(0,4)
var new_year = year-1;
if(month < 10)
month = '0' + month.toString();
var x=2
totalmonth=parseInt(month)+parseInt(x)
var datefrom = new_year + '-' + month + '-' + day;
//$('#date_35').attr('min', maxDate);
var txtVal = $('#txtDate').val();
if(isDate(txtVal)){
alert('Valid Date');
}
else{
$("#txtDate").addClass("danger");
document.getElementById("txtDate").focus();
$("#txtDate_error").html("Please Enter Correct Date Format");
// alert('Invalid Date');
return false;
}
}
function isDate(txtDate)
{
var currVal = txtDate;
if(currVal == '')
return false;
var rxDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/; //Declare Regex
// var rxDatePattern = /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/;
var dtArray = currVal.match(rxDatePattern); // is format OK?