JavaScript new Date Ordinal (st, nd, rd, th)

前端 未结 15 1958
北海茫月
北海茫月 2020-11-27 03:12

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:

<
15条回答
  •  天涯浪人
    2020-11-27 03:50

    Here is an easy solution:

    var date = today.getDate() + (today.getDate() % 10 == 1 && today.getDate() != 11 ? + 'st': (today.getDate() % 10 == 2 && today.getDate() != 12 ? + 'nd': 
    
    (today.getDate() % 10 == 3 && today.getDate() != 13 ? + 'rd':'th')));
    

提交回复
热议问题