当我们在date对象上调用getMonth()和getDate()时,我们将得到一个个位数。 例如:
对于一月份,它显示为1,但我需要将其显示为01。怎么做呢?
当我们在date对象上调用getMonth()和getDate()时,我们将得到一个个位数。 例如:
对于一月份,它显示为1,但我需要将其显示为01。怎么做呢?
当前回答
以下命令用于转换db2日期格式 即YYYY-MM-DD使用三元运算符
var currentDate = new Date();
var twoDigitMonth=((currentDate.getMonth()+1)>=10)? (currentDate.getMonth()+1) : '0' + (currentDate.getMonth()+1);
var twoDigitDate=((currentDate.getDate())>=10)? (currentDate.getDate()) : '0' + (currentDate.getDate());
var createdDateTo = currentDate.getFullYear() + "-" + twoDigitMonth + "-" + twoDigitDate;
alert(createdDateTo);
其他回答
如果你想要一个像"YYYY-MM-DDTHH:mm:ss"这样的格式,那么这可能会更快:
var date = new Date().toISOString().substr(0, 19);
// toISOString() will give you YYYY-MM-DDTHH:mm:ss.sssZ
或者常用的MySQL日期时间格式“YYYY-MM-DD HH:mm:ss”:
var date2 = new Date().toISOString().substr(0, 19).replace('T', ' ');
这就是我的解决方案:
function leadingZero(value) {
if (value < 10) {
return "0" + value.toString();
}
return value.toString();
}
var targetDate = new Date();
targetDate.setDate(targetDate.getDate());
var dd = targetDate.getDate();
var mm = targetDate.getMonth() + 1;
var yyyy = targetDate.getFullYear();
var dateCurrent = leadingZero(mm) + "/" + leadingZero(dd) + "/" + yyyy;
三元运算符解
如果月份或日期小于10,则简单的三元运算符可以在数字之前添加“0”(假设需要在字符串中使用此信息)。
let month = (date.getMonth() < 10) ? "0" + date.getMonth().toString() : date.getMonth();
let day = (date.getDate() < 10) ? "0" + date.getDate().toString() : date.getDate();
如果你能给我点时间的话我想问的是
YYYYMMDD
为了今天,和大家相处:
const dateDocumentID = new Date()
.toISOString()
.substr(0, 10)
.replace(/-/g, '');
$("body").delegate("select[name='package_title']", "change", function() {
var price = $(this).find(':selected').attr('data-price');
var dadaday = $(this).find(':selected').attr('data-days');
var today = new Date();
var endDate = new Date();
endDate.setDate(today.getDate()+parseInt(dadaday));
var day = ("0" + endDate.getDate()).slice(-2)
var month = ("0" + (endDate.getMonth() + 1)).slice(-2)
var year = endDate.getFullYear();
var someFormattedDate = year+'-'+month+'-'+day;
$('#price_id').val(price);
$('#date_id').val(someFormattedDate);
});