javascript - How to convert date --- Wed Feb 27 2019 11:11:16 GMT+0530 (India Standard Time) to 27-Feb-2019 format in React JS -

I am getting date as Wed Feb 27 2019 11:11:16 GMT+0530 (India Standard Time) from date-picker. Now I ha

I am getting date as Wed Feb 27 2019 11:11:16 GMT+0530 (India Standard Time) from date-picker. Now I have convert it to 27-Feb-2019 format. Please suggest me any simplest way for this.

var d=(date); alert(d);

enter image description here

I am getting date as Wed Feb 27 2019 11:11:16 GMT+0530 (India Standard Time) from date-picker. Now I have convert it to 27-Feb-2019 format. Please suggest me any simplest way for this.

var d=(date); alert(d);

enter image description here

Share Improve this question asked Mar 5, 2019 at 5:48 Sameer PatkarSameer Patkar 1012 silver badges9 bronze badges 1
  • use Date object in javscript - if that doesn't do it for you, then there's always libraries like moment.js that make working with dates easy - also, use a better date picker that returns a Date object rather than a string – Jaromanda X Commented Mar 5, 2019 at 5:50
Add a ment  | 

4 Answers 4

Reset to default 4

use moment library

console.log(moment(new Date('Wed Feb 27 2019')).format('DD-MMM-YYYY'));

console.log(moment(new Date('Wed Feb 27 2019 11:11:16 GMT+0530 (India Standard Time)')).format('DD-MMM-YYYY'));
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.24.0/moment.min.js"></script>

You can use moment/dayjs/date-fns or similar libraries to format the date in a format you want.

An example with momentJs

moment('2014-08-20 15:30:00').format('DD-MMM-YYYY')

Please Try this.

  var  monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "July", "Aug", "Sep", "Oct", "Nov", "Dec"
];
   var fromDate = new Date('Wed Feb 27 2019 11:11:16 GMT+0530 (India Standard Time)');
 
  var date =fromDate.getDate()+'-'+monthNames[fromDate.getMonth()]+'-'+fromDate.getFullYear();
alert(date )
console.log(date )

Edit:

  1. The getDate() method returns the day of the month (from 1 to 31) for the specified date.

  2. The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time. Note: January is 0, February is 1, and so on.

  3. The getFullYear() method returns the year (four digits for dates between year 1000 and 9999) of the specified date.

It does not specifically require React

First create an object for mapping the month number with month name. Then using Date object convert the input and use getDate , getMonth & getFullYear functions to create the date in required format.

Also note the month starts with 0 , that mean Jan is 0 so adding 1 to it

let mnthObj = {
  1: 'Jan',
  2: 'Feb',
  3: 'March',
  4: 'April'

}
let dt = new Date('Wed Feb 27 2019 11:11:16 GMT+0530 (India Standard Time)');

let conDtObj = `${dt.getDate()}-${mnthObj[dt.getMonth()+1]}-${dt.getFullYear()}`
console.log(conDtObj)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744967109a4603748.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信