If at all possible, without JavaScript libraries or lots of clunky code I am looking for the simplest way to format a date two weeks from now in the following format:
<I'm a bit late to the party, but this should work:
function ordinal(number) {
number = Number(number)
if(!number || (Math.round(number) !== number)) {
return number
}
var signal = (number < 20) ? number : Number(('' + number).slice(-1))
switch(signal) {
case 1:
return number + 'st'
case 2:
return number + 'nd'
case 3:
return number + 'rd'
default:
return number + 'th'
}
}
function specialFormat(date) {
// add two weeks
date = new Date(+date + 12096e5)
var months = [
'January'
, 'February'
, 'March'
, 'April'
, 'May'
, 'June'
, 'July'
, 'August'
, 'September'
, 'October'
, 'November'
, 'December'
]
var formatted = ordinal(date.getDate())
formatted += ' ' + months[date.getMonth()]
return formatted + ' ' + date.getFullYear()
}
document.body.innerHTML = specialFormat(new Date())
Lots of formatting answers, so I'll just work on the nth of any integer-
Number.prototype.nth= function(){
if(this%1) return this;
var s= this%100;
if(s>3 && s<21) return this+'th';
switch(s%10){
case 1: return this+'st';
case 2: return this+'nd';
case 3: return this+'rd';
default: return this+'th';
}
}
A short and compact solution:
function format(date, tmp){
return [
(tmp = date.getDate()) +
([, 'st', 'nd', 'rd'][/1?.$/.exec(tmp)] || 'th'),
[ 'January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'
][date.getMonth()],
date.getFullYear()
].join(' ')
}
// 14 days from today
console.log('14 days from today: ' +
format(new Date(+new Date + 14 * 864e5)));
// test formatting for all dates within a month from today
var day = 864e5, today = +new Date;
for(var i = 0; i < 32; i++) {
console.log('Today + ' + i + ': ' + format(new Date(today + i * day)))
}
(The compact regex-based approach for getting the ordinal suffix appears several places around the web, original source unknown)
Strongly inspired by @user2309185's.
const ordinal = (d) => {
return d + (['st', 'nd', 'rd'][d % 10 - 1] || 'th')
}
Lots of answers, here's another:
function addOrd(n) {
var ords = [,'st','nd','rd'];
var ord, m = n%100;
return n + ((m > 10 && m < 14)? 'th' : ords[m%10] || 'th');
}
// Return date string two weeks from now (14 days) in
// format 13th March 2013
function formatDatePlusTwoWeeks(d) {
var months = ['January','February','March','April','May','June',
'July','August','September','October','November','December'];
// Copy date object so don't modify original
var e = new Date(d);
// Add two weeks (14 days)
e.setDate(e.getDate() + 14);
return addOrd(e.getDate()) + ' ' + months[e.getMonth()] + ' ' + e.getFullYear();
}
alert(formatDatePlusTwoWeeks(new Date(2013,2,13))); // 27th March 2013
If you are a fan of moment.js, then you can make it with format("Do")
Examples
var newdate = new Date();
moment(newdate).format("Do MMMM YYYY")
//Returns 1st January 2020
moment("01/01/2020", "MM/DD/YYYY").format("Do")
//Returns 1st
moment("01/01/2020", "MM/DD/YYYY").format("Do MMM YYYY")
//Returns 1st Jan 2020