我想在javascript中将用户输入日期格式更改为字符串

问题描述 投票:-3回答:1

我是javascript的新手,我有这个日期值(出生= 1986-12-14),我想把它改成字符串,但我得到的只是减法操作。

var birth = 1986-12-14;
console.log(birth.toString());

output: 1960 

我希望结果是“1986-12-14”

javascript
1个回答
2
投票

var birth = 1986-12-14;制作birth = 1960,因为1986-12-14不是约会,它是一个数学表达式。

如果你想要约会,请使用date

注意:月份表示为从0开始的整数。所以,如果你想要12月,你可以使用11这个月。

var birth = new Date(1986,11,14);

// Extract the parts of the date that you want and build a string from them.
// Adjust the month for human consumption
console.log(birth.getFullYear() + "-" + (birth.getMonth() + 1) + "-" + birth.getDate());
© www.soinside.com 2019 - 2024. All rights reserved.