在momentjs中将日期从给定格式转换为yyyy-mm-dd

时间:2018-10-22 11:03:26

标签: javascript momentjs

我有这种格式的日期。我正在使用monentjs的format()。 当前mydate的格式为2018-10-11T16:31:27 + 05:30

let newdate=let newdate=this.state.startDate.format();

我想要此日期为2018-10-22的格式。 我尝试使用

let formatfun=(date)=>{
      let x=moment(date,"moment.HTML5_FMT.DATE");
      return x;
    }

但无法到达那里。

5 个答案:

答案 0 :(得分:0)

const newdate = "2018-10-11T16:31:27+05:30"; // this.state.startDate.format();
const formatfun = date => {
    return date.substr(0, 10);
}
console.log(formatfun(newdate));

这会有所帮助吗?只需获取日期字符串的前10个字符。

或者如果它是日期类型,请查看Format JavaScript Date to yyyy-mm-dd(可能重复)。

答案 1 :(得分:0)

您尝试过吗?

let formatfun=(date)=>{
  let x=moment(date).format("YYYY-MM-DD");
  return x;
}

答案 2 :(得分:0)

您可以了解有关formatting with moment.js here.

的更多信息

let formatfun = (date) => {
   let x = moment(date).format('YYYY-MM-DD');
   return x;
}

let date = '2018-10-22T16:31:27+05:30';
console.log(date);
console.log(formatfun(date));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

答案 3 :(得分:0)

尝试这种方式

  

moment(“ yourdate”,“ dateformat”)。format(“ newformat”);

let formatfun=(date)=>{
      let x=moment(date,"moment.HTML5_FMT.DATE").format('yyyy-dd-mm');
      return x;
}

在此处了解更多信息:https://momentjs.com/docs/#/parsing/string-format/

答案 4 :(得分:0)

您可以使用moment(newdate).format(“ YYYY-MM-DD”)

检查以下网址以获取更多参考信息:-

https://momentjs.com/docs/#/parsing/string-format/