JavaScript new Date Ordinal(st,nd,rd,th) [英] JavaScript new Date Ordinal (st, nd, rd, th)

查看:171
本文介绍了JavaScript new Date Ordinal(st,nd,rd,th)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果可能的话,没有JavaScript库或许多笨重的代码,我正在寻找最简单的方法来格式化一个日期,从现在起以下列格式:

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:

2013年3月13日

我使用的代码是:

var newdate = new Date(+new Date + 12096e5);
document.body.innerHTML = newdate;

返回两周后的日期和时间,但是像这样:
Wed Mar 27 2013 21:50:29 GMT + 0000(GMT标准时间)

which returns the date and time two weeks from now, but like this: Wed Mar 27 2013 21:50:29 GMT+0000 (GMT Standard Time)

以下是 jsFiddle

任何帮助都将不胜感激!

Any help would be appreciated!

推荐答案

这里:

JSFiddle

var fortnightAway = new Date(+new Date + 12096e5),
  date = fortnightAway.getDate(),
  month = ["January","February","March","April","May","June","July",
           "August","September","October","November","December"][fortnightAway.getMonth()];

function nth(d) {
  if (d > 3 && d < 21) return 'th'; 
  switch (d % 10) {
    case 1:  return "st";
    case 2:  return "nd";
    case 3:  return "rd";
    default: return "th";
  }
}
document.body.innerHTML = date + nth(date) + " " +
  month + " " + 
  fortnightAway.getFullYear();

这篇关于JavaScript new Date Ordinal(st,nd,rd,th)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆