14
<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

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

2018/02/01 14:16:00

1

9 Answers 9

23

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

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));

Sign up to request clarification or add additional context in comments.

3 Comments

That's great! Glad I helped!
but result , 01.02.2018 14:48:00 i try sequential : 20180201144800 like this, is it possible?
Format YYYY/MM/DD :: new Intl.DateTimeFormat( 'fr-ca' ).format( new Date( ) ).replace(/-/g,'/');
23

Use date-fns library

for "2018/02/01 14:16:00" this format use the following code

import { format } from 'date-fns';
console.log(format(new Date(), 'yyyy/MM/dd kk:mm:ss'))

with this you can customize your own format.

date-fns

For customizing date format use this

2 Comments

This is the simplest and shortest way
It would be nice to add plugin link, so that viewer doesn't need to search for it.
8

I found the easiest solution for the React.js developer.

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

createdAt: {
       type: Date,
       default: 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

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.

2 Comments

you can add some link to see all the documentation and implementation. Thanks!
3

Just use moment and define the format as below

moment(yourDate).format("YYYY/MM/DD kk:mm:ss");

1 Comment

Moment.js no longer maintained, please read their announcement here: momentjs.com/docs/#/-project-status
1

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(expirationdate, "yyyy-mm-dd")

Comments

0
  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);

Comments

0

Moment JS provide control on each date string and you can easily convert into another format easily.

Install Moment Package:

npm install --save moment

Convert date format using moment:

import Moment from 'moment';
  
class App extends Component {
  constructor() {
  
    this.state = {
      dateDMY: Moment("1994-07-01").format('DD-MM-YYYY'),
      dateMDY: Moment("1994-07-01").format('MM-DD-YYYY'),
      dateYMD: Moment("01-07-1994").format('YYYY-MM-DD')
    }
  }
  
  render() {
    return (
      <div>
        <p> DMY Format: { this.state.dateDMY } </p>
        <p> MDY Format: { this.state.dateMDY } </p>
        <p> YMD Format: { this.state.dateYMD } </p>
      </div>
    );
  }
}
   
render(<App />, document.getElementById('root'));

Output:

DMY Format: 01-07-1994
MDY Format: 07-01-1994
YMD Format: 1994-01-07

2 Comments

momentjs is no longer maintained
We can use npmjs.com/package/date-fns as an alternative to moment-js
0

Since moment is no longer maintained, you can now use dayjs:

dayjs("2019-04-30T08:59:00.000Z").format("YYYY/MM/DD hh:mm:ss")

Comments

-1

I used below logic, It may help you.

{new Date(your date).toLocaleString("lookup")}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.