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

zeaolanos
  • 193
  • 1
  • 1
  • 9
  • Does this answer your question? [How do I format a date in JavaScript?](https://stackoverflow.com/questions/3552461/how-do-i-format-a-date-in-javascript) – Liam Jul 25 '22 at 11:53

8 Answers8

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));
Rodius
  • 2,271
  • 14
  • 19
16

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

siva surya
  • 613
  • 7
  • 12
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.
fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88
surbhi241
  • 81
  • 1
  • 2
3

Just use moment and define the format as below

moment(yourDate).format("YYYY/MM/DD kk:mm:ss");
Musadul Islam
  • 341
  • 3
  • 11
1
  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);
zeaolanos
  • 193
  • 1
  • 1
  • 9
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")
The pyramid
  • 313
  • 1
  • 6
  • 18
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
Ankit Jindal
  • 3,672
  • 3
  • 25
  • 37
-1

I used below logic, It may help you.

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