javascript 如何在javascript reactjs中转换日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48621533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:08:26  来源:igfitidea点击:

how convert date format in javascript reactjs

javascriptreactjsnpm

提问by zeaolanos

<b>Select Datetime</b><br/>
<DateTimeField onChange={this.clockevent} format={"x"}/>   

clockevent=(newDate)=>{
    var dateVal ="/Date("+newDate+")/";
    var date = new Date(parseFloat(dateVal.substr(6)));


    console.log(
    date.getFullYear() + " " +
    (date.getMonth() + 1) + "/" +
    date.getDate() + "  " +

    date.getHours() + ":" +
    date.getMinutes() + ":" +
    date.getSeconds() );
}

result :

结果 :

2018/2/1 14:16:0

2018/2/1 14:16:0

i want the result add day,month and ss ' format (DD MM ss) i want to this format =

我想要结果添加日、月和 ss '格式(DD MM ss)我想要这种格式 =

2018/02/01 14:16:00

2018/02/01 14:16:00

回答by Rodius

I haven't found the way to format YYYY/MM/DD as you asked but I have the 2-digit format.

我还没有找到你问的格式化 YYYY/MM/DD 的方法,但我有 2 位格式。

const today = Date.now();

console.log(new Intl.DateTimeFormat('en-US', {year: 'numeric', month: '2-digit',day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit'}).format(today));

回答by zeaolanos

  i did.  
        var dateVal ="/Date("+newDate+")/";
        var date = new Date(parseFloat(dateVal.substr(6)));
        const YYYY = date.getFullYear();
        let DD = date.getMonth()+1;
        let MM = date.getDate();
        let HH = date.getHours() ;
        let mm = date.getMinutes()
        let ss = date.getSeconds();


        if(DD<10)
        {
            DD=`0${DD}`;
        }
        if(MM<10)
        {
            MM=`0${MM}`;
        }

        if(HH<10)
        {
            HH=`0${HH}`;
        }
        if(mm<10)
        {
            mm=`0${mm}`;
        }
        if(ss<10)
        {
            ss=`0${ss}`;
        }

        const ltime= (YYYY+DD+MM+HH+mm+ss);

回答by The pyramid

You can use dateformat lib (https://github.com/felixge/node-dateformat) , it has so many options that can help you , and it's easy to use , you can use it in front-end or back-end , it worked fine for me here an example :

你可以使用dateformat lib(https://github.com/felixge/node-dateformat),它有很多可以帮助你的选项,而且它很容易使用,你可以在前端或后端使用它,这里的一个例子对我来说很好用:

dateFormat(expirationdate, "yyyy-mm-dd")

回答by surbhi241

I found the easiest solution for the reactjs developer.

我为 reactjs 开发人员找到了最简单的解决方案。

In my schema(In the backend) date format was like this.

在我的架构中(在后端)日期格式是这样的。

createdAt: { type: Date, default: Date.now() }

createdAt: { 类型:日期,默认值:Date.now() }

it will give me output as 2019-04-30T08:59:00.000Z which is (YYYY-MM-DD'T'HH:MM:SS.SSSZ) format. To change the date format in Frontend you can easily solve the problem using

它会给我输出 2019-04-30T08:59:00.000Z 这是 (YYYY-MM-DD'T'HH:MM:SS.SSSZ) 格式。要在前端更改日期格式,您可以使用以下方法轻松解决问题

  1. npm install dateformat
  2. import dateFormat from 'dateformat';
  3. dateFormat("2019-04-30T08:59:00.000Z", "mmmm dS, yyyy") to get April 30th, 2019 or dateFormat("2019-04-30T08:59:00.000Z", "dddd, mmmm dS, yyyy") to get Tuesday, April 30th, 2019.
  1. npm 安装日期格式
  2. 从“日期格式”导入日期格式;
  3. dateFormat("2019-04-30T08:59:00.000Z", "mmmm dS, yyyy") 获取 2019 年 4 月 30 日或 dateFormat("2019-04-30T08:59:00.000Z", "dddd, mmmm dS" yyyy") 到 2019 年 4 月 30 日,星期二。