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"));

return format;
}
console.log('Converted date: '+ dateFormat('12/10/2022', 'dd-MM-yyyy'));

How to set min date & Max date in datepicker

File name : index.php

<script type="text/javascript">

$( 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);


$('#date_from').attr('min',"2018-01-01");
$('#date_from').attr('max',today);

$('#date_to').attr('min',"2018-01-01");
$('#date_to').attr('max',today);

$('#date_from').val(datefrom);
$('#date_to').val(today);


});
</script>

How to validate date format using jquery.

Date Format (dd/mm/YYYY) OR (mm/dd/YYYY) , (dd-mm-YYYY) OR (mm-dd-YYYY)

<style>
.danger{
/*background-color: #ff0000;*/
color: #000000;
border: 3px solid #ff0000;
}
</style> <input type="text" id="txtDate" onblur="dateformat()"/> (dd/mm/yyyy) OR (dd-mm-YYYY)


<span class="error" id="txtDate_error"></span> <script type="text/javascript">

function dateformat() {

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?

if (dtArray == null)
return false;

//Checks for mm/dd/yyyy format.
/*dtMonth = dtArray[1];
dtDay= dtArray[3];
dtYear = dtArray[5]; */

//Checks for dd/mm/yyyy format.
dtDay = dtArray[1];
dtMonth= dtArray[3];
dtYear = dtArray[5];

if (dtMonth < 1 || dtMonth > 12)
return false;
else if (dtDay < 1 || dtDay> 31)
return false;
else if ((dtMonth==4 || dtMonth==6 || dtMonth==9 || dtMonth==11) && dtDay ==31)
return false;
else if (dtMonth == 2)
{
var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
if (dtDay> 29 || (dtDay ==29 && !isleap))
return false;
}
return true;
}


</script>





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here