最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Format date with Ordinal Number Suffix - Stack Overflow

matteradmin4PV0评论

I would like to format my date as the following, f.ex.: Thursday, May 2nd 2019.

I created the following function:

function convertDateToString(date) {
  let monthNames = [
    "January", "February", "March",
    "April", "May", "June", "July",
    "August", "September", "October",
    "November", "December"
  ];

  let dayNames = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

  let dayIndex = date.getDate();
  let monthIndex = date.getMonth();
  let year = date.getFullYear();

  return dayNames[dayIndex] + ', ' + monthNames[monthIndex] + ' ' + dayIndex + ' ' + year;
}

let date = new Date()

console.log(convertDateToString(date));

// wanted format: Thursday, May 2nd 2019

I would like to format my date as the following, f.ex.: Thursday, May 2nd 2019.

I created the following function:

function convertDateToString(date) {
  let monthNames = [
    "January", "February", "March",
    "April", "May", "June", "July",
    "August", "September", "October",
    "November", "December"
  ];

  let dayNames = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

  let dayIndex = date.getDate();
  let monthIndex = date.getMonth();
  let year = date.getFullYear();

  return dayNames[dayIndex] + ', ' + monthNames[monthIndex] + ' ' + dayIndex + ' ' + year;
}

let date = new Date()

console.log(convertDateToString(date));

// wanted format: Thursday, May 2nd 2019

However, I am not sure how to add the ordinal number suffix to the date. I currently only get the full number and not the 1st, 2nd etc.

Any suggestions how to add this?`

I appreciate your replies!

Share Improve this question asked Jun 4, 2019 at 6:24 Carol.KarCarol.Kar 5,23538 gold badges148 silver badges298 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

You can create a function which takes in the day of the month, eg 2, and returns the appropriate suffix, eg nd (or st in the case of 1 (or 21 or 31), rd in the case of 3 (or 23), and th in all other cases):

const getSuffix = (num) => {
  const suffixes = {
    '1': 'st',
    '21': 'st',
    '31': 'st',
    '2': 'nd',
    '22': 'nd',
    '3': 'rd',
    '23': 'rd'
  };
  return suffixes[num] || 'th';
};
function convertDateToString(date) {
  let monthNames = [
    "January", "February", "March",
    "April", "May", "June", "July",
    "August", "September", "October",
    "November", "December"
  ];

  let dayNames = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

  let dayIndex = date.getDate();
  let monthIndex = date.getMonth();
  let year = date.getFullYear();

  return dayNames[dayIndex] + ', ' + monthNames[monthIndex] + ' ' + dayIndex + getSuffix(dayIndex) + ' ' + year;
}

let date = new Date()

console.log(convertDateToString(date));

// wanted format: Thursday, May 2nd 2019

// Takes a number and return with ordinal suffix
const ordinal = (n) => {
  const s = ['th', 'st', 'nd', 'rd'];
  const m = n % 100;
  return n + (s[(m - 20) % 10] || s[m] || s[0]);
};

// See for yourself
for (let i = 1; i < 50; i++) {
    console.log(ordinal(i));
}

You can create an array of suffixes.

function convertDateToString(date) {
  let monthNames = [
    "January", "February", "March",
    "April", "May", "June", "July",
    "August", "September", "October",
    "November", "December"
  ];
  let suffix = ['st','nd','rd',...Array(13).fill('th'),'st','nd','rd',Array(7).fill('th'),'st']
  let dayNames = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

  let dayIndex = date.getDate();
  let monthIndex = date.getMonth();
  let year = date.getFullYear();

  return dayNames[dayIndex] + ', ' + monthNames[monthIndex] + ' ' + dayIndex + suffix[dayIndex-1] + ' ' + year;
}

let date = new Date()

console.log(convertDateToString(date));

// wanted format: Thursday, May 2nd 2019

Worth noting that any time you deal with dates, it's worth considering including Moment.js - it's a library that makes working with dates much simpler.

You can format your display in the following manner:

function convertDateToString(date) {
  return moment(date)
    .format("dddd, MMMM Do YYYY")
}

let date1 = new Date("2019-05-01T10:00:00")
let date2 = new Date("2019-05-02T10:00:00")
let date3 = new Date("2019-05-03T10:00:00")
let date4 = new Date("2019-05-04T10:00:00")

console.log(convertDateToString(date1));
console.log(convertDateToString(date2));
console.log(convertDateToString(date3));
console.log(convertDateToString(date4));

// wanted format: Thursday, May 2nd 2019
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.24.0/moment.min.js"></script>

For more formatting options, you can check the documentation.

Post a comment

comment list (0)

  1. No comments so far