jquery format date code example
Example 1: javascript convert date to yyyy-mm-dd
const formatYmd = date => date.toISOString().slice(0, 10);
formatYmd(new Date());
Example 2: jquery format date
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}
alert(formatDate('05/08/2015'));
Example 3: jquery validator Date
$.validator.addMethod("birth", function (value, element) {
var ActualDate = new Date();
var ActualYear = ActualDate.getFullYear();
var ActualMonth = ActualDate.getMonth();
var ActualDay = ActualDate.getUTCDate();
ActualMonth += 1;
var Data_Inserida = value.split("-");
var year = parseInt(Data_Inserida[0], 10),
month = parseInt(Data_Inserida[1], 10),
day = parseInt(Data_Inserida[2], 10);
if (year == ActualYear) {
if (month == ActualMonth) {
if (day > ActualDay) {
return false;
} else
return true
} else {
if (month > ActualMonth)
return false;
else
return true;
}
}
if (year > (ActualYear - 66) && year < ActualYear-18) {
return true;
} else
return false;
return false;
})
Example 4: how make date object in jquery from custom date
var formattedDate = new Date("yourUnformattedOriginalDate");
var d = formattedDate.getDate();
var m = formattedDate.getMonth();
m += 1;
var y = formattedDate.getFullYear();
$("#txtDate").val(d + "." + m + "." + y);
Example 5: jquery date format
$.date = function(dateObject) {
var d = new Date(dateObject);
var day = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
if (day < 10) {
day = "0" + day;
}
if (month < 10) {
month = "0" + month;
}
var date = day + "/" + month + "/" + year;
return date;
};