I need to get the date format as \'DD-Mon-YYYY\' in javascript. I had asked a question, and it got marked duplicate to jQuery date formatting
But, the answers provi
//convert DateTime result in jquery mvc 5 using entity fremwork
const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
function DateAndTime(date) {
var value = new Date
(
parseInt(date.replace(/(^.*\()|([+-].*$)/g, ''))
);
var dat = value.getDate() +
"-" +
monthNames[value.getMonth()] +
"-" +
value.getFullYear();
var hours = value.getHours();
var minutes = value.getMinutes();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return { Date: dat, Time: strTime };
}
// var getdate = DateAndTime(StartDate);
//var Date = getdate.Date;//here get date
//var time = getdate.Time;//here get Time
//alert(Date)
/*
#No parameters
returns a date with this format DD-MM-YYYY
*/
function now()
{
var d = new Date();
var month = d.getMonth()+1;
var day = d.getDate();
var output = (day<10 ? '0' : '') + day + "-"
+ (month<10 ? '0' : '') + month + '-'
+ d.getFullYear();
return output;
}
You can use toLocaleDateString and hunt for a format that's close to DD-mmm-YYYY (hint: 'en-GB'; you just need to replace the spaces with '-').
const date = new Date();
const formattedDate = date.toLocaleDateString('en-GB', {
day: 'numeric', month: 'short', year: 'numeric'
}).replace(/ /g, '-');
console.log(formattedDate);
I've made a custom date string format function, you can use that.
var getDateString = function(date, format) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
getPaddedComp = function(comp) {
return ((parseInt(comp) < 10) ? ('0' + comp) : comp)
},
formattedDate = format,
o = {
"y+": date.getFullYear(), // year
"M+": months[date.getMonth()], //month
"d+": getPaddedComp(date.getDate()), //day
"h+": getPaddedComp((date.getHours() > 12) ? date.getHours() % 12 : date.getHours()), //hour
"H+": getPaddedComp(date.getHours()), //hour
"m+": getPaddedComp(date.getMinutes()), //minute
"s+": getPaddedComp(date.getSeconds()), //second
"S+": getPaddedComp(date.getMilliseconds()), //millisecond,
"b+": (date.getHours() >= 12) ? 'PM' : 'AM'
};
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
formattedDate = formattedDate.replace(RegExp.$1, o[k]);
}
}
return formattedDate;
};
And now suppose you've :-
var date = "2014-07-12 10:54:11";
So to format this date you write:-
var formattedDate = getDateString(new Date(date), "d-M-y")
Use date format dd-MM-yy . It will output like: 16-December-2014.
Here's a simple solution, using TypeScript:
convertDateStringToDate(dateStr) {
// Convert a string like '2020-10-04T00:00:00' into '4/Oct/2020'
let months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
let date = new Date(dateStr);
let str = date.getDate()
+ '/' + months[date.getMonth()]
+ '/' + date.getFullYear()
return str;
}
(Yeah, I know the question was about JavaScript, but I'm sure I won't be the only Angular developer coming across this article !)