是否有内置函数或插件来处理JavaScript中的日期格式? [英] Is there a built-in function or plugin to handle date formatting in JavaScript?
问题描述
目前,我需要在2012年10月5日类型格式中输出日期。意思是月份,没有前导零,空格,全月份名,逗号,空格,四位数年份。我需要在JavaScript中执行此操作。我有这个工作,但它发生在我写的冗长的代码,有人必须已经找出了一个更好的方式来做到这一点。
Currently I need to output a date in a: '5 October, 2012' type format. Meaning day-of-month with no leading zeros, space, full month name, comma, space, four-digit year. I need to do this in JavaScript. I have this working but it occurs to me while writing the lengthy code that somebody must've already figured out a better way to do this.
我不认为那里是一种内置的JavaScript功能,可以正确地对我进行格式化。 PHP中只有一件事, date()
。有没有JavaScript插件可以做同样的事情?
I don't think there is a built in function of JavaScript that formats this exactly how I want. There is just a thing in PHP with date()
. Is there a plugin for JavaScript that does the same thing?
为了给出一个具体的例子,在这种情况下,我从未来的几个小时开始我需要得到日期。
For the sake of giving a specific example, in this instance I start with a set number of hours into the future that I need to get the date for.
目前我有:
var myNow = new Date().getTime();
var myTime = hours * 60 * 60 * 1000;
var myDate = new Date(myTime + myNow);
var myDay = myDate.getDate();
var myMonthNum = myDate.getMonth();
var myMonth = '';
var myYear = myDate.getFullYear();
switch(myMonthNum) {
case 0:
myMonth = 'January';
break;
...
var completeDate = myDate = " " + myMonth + ", " + myYear;
$('#theEndDate').html(completeDate);
推荐答案
不,没有内置的 日期
对象,但是有一堆图书馆来处理和格式化:
No, there is nothing built-in for Date
objects, but there are a bunch of libraries to deal with and format them:
- date.js
- 时刻。 js
- XDate
- 日期和 Date.Extras
- Date in Sugar
- Dojo.date
- 几个功能在Mochikit
- DateFormat (只有PHP的
日期
) - date 在php.js
- DataType ,尤其是i18n
- date.js
- moment.js
- XDate
- Date and Date.Extras in Mootools
- Date in Sugar
- Dojo.date
- a few functions in Mochikit
- DateFormat (only PHP's
date
) - date at php.js
- DataType in YUI, especially for i18n
这篇关于是否有内置函数或插件来处理JavaScript中的日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!