I am trying to display date in dd/mm/yyyy and the value should be store as yyyymmdd in a variable.
dd/mm/yyyy is displayed correct but the value is not storing in format yyyymmdd it is showing as yyyymd
like if I select 02/03/2022 it is storing as 202232 which is incorrect as it has to be store as 20220302.
var strDateTimeEntry;
$(function () {
$("#entrydate").datepicker({
//date format for displaying
dateFormat: "dd/mm/yy",
});
$("#entrydate").change(function () {
var date = $(this).datepicker("getDate");
//date format for storing
strDateTimeEntry = date.getFullYear() + "" + (date.getMonth() + 1) + "" + date.getDate();
$("#EntryDateDisplay").text(strDateTimeEntry);
alert(strDateTimeEntry);
});
});
You just need to pad your month and day.
strDateTimeEntry = date.getFullYear() + "" + (date.getMonth() + 1).toString().padStart(2, '0') + "" + date.getDate().toString().padStart(2, '0');
Here's a fiddle example that takes a Date object and displays output in the required format.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.