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
4 Answers
Reset to default 4use 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:
The getDate() method returns the day of the month (from 1 to 31) for the specified date.
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.
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条)