how convert date format in javascript reactjs

<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

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

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

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

Just use moment and define the format as below

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

You can use dateformat lib () , 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")
 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);

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

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

I used below logic, It may help you.

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like