提问者:小点点

toISOString方法返回前面有零的年份


此功能:

 handleChange(e) {
    this.setState({
      [e.target.name] : e.target.value,
      displayName: (this.state.firstName.substring(0,2)) + (this.state.lastName.substring(0,2)),
      orderNo: (this.props.children.length + 1).toString(),
      sortBirthDate: this.state.birthDate ? new Date(this.state.birthDate).toISOString() :this.state.birthDate ,
      userId: "8e1d8d"
    });
  }

正确返回所有内容,但sortbirthdate除外:

birthDate: "09/21/2014"
displayName: "dsda"
firstName: "ds"
lastName: "da"
orderNo: "5"
sortBirthDate: "0201-09-21T05:50:36.000Z"

我曾尝试使用substring方法移除零,但返回的年份只有3位数字。

有人看到我在这里遗漏了什么吗?

提前谢谢你!


共1个答案

匿名用户

尝试以下操作,它将返回您想要的正确输出:-

null

// Here nothing has been assigned 
// while creating Date object 
var dateobj = new Date('09/21/2014'); 
  
// Contents of above date object is 
// converted into a string using toISOString() function. 
var B = dateobj.toISOString(); 
  
// Printing the converted string. 
document.write(B);