使用 jquery/javascript 从日期中获取日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8771114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting day from date using jquery/javascript
提问by Om3ga
I have a date in this formate "YYYY/MM/DD". Now I want to get the day from this format. How can I get it using javascript or jquery? One way is this
我在这个格式中有一个日期“YYYY/MM/DD”。现在我想从这种格式中获得一天。如何使用 javascript 或 jquery 获取它?一种方法是这样
$(document).ready(function() {
var d = new Date();
alert(d.getDay());
} );
but the problem here is that d variable contains date in this format
但这里的问题是 d 变量包含这种格式的日期
Sat Jan 07 2012 18:16:57 GMT+0200 (FLE Standard Time)
How can I get day from date in above format?
如何以上述格式从日期中获取日期?
Here is how my function look like
这是我的功能的样子
function dayOfWeek(d) {
var dayNames = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Sunday');
var currentDate = Math.round(+new Date(d)/1000);
var nData = new Date(currentDate);
//alert(nData);
//alert(nData.getDay());
//var day = d.split("-")[2];
//var t = day.replace("0","");
//alert(t);
return dayNames[nData.getDay()];
}
What I am doing is I am passing date in "YYYY-MM-DD" format and it converts that date to unix timestamp and then I get the day and then return the day as Sunday or Monday etc. But here it only returns Friday which is incorrect. When I change the day in date it should return the same day in array.
我正在做的是我以“YYYY-MM-DD”格式传递日期,并将该日期转换为unix时间戳,然后获取日期,然后将日期返回为星期日或星期一等。但在这里它只返回星期五是不正确的。当我更改日期时,它应该在数组中返回同一天。
采纳答案by FK82
The Date
Object
has a toString
method which outputs a UTC date string in the format you described. However the Date
Object
also has a host of other methods.
所述Date
Object
具有toString
其输出在您所描述的格式的UTC日期字符串方法。然而,Date
Object
也有许多其他方法。
To get the day of the month (1-31) you use the getDate
method:
d = new Date( ).getDate( ) ;
要获取月份中的第几天 (1-31),您可以使用以下getDate
方法:
d = new Date( ).getDate( ) ;
Your date string has a format that will not get recognized by the Date
string constructor. Test this line for its return value:
您的日期字符串的格式不会被Date
字符串构造函数识别。测试此行的返回值:
var nData = new Date(currentDate) ;
It will return:
它将返回:
"invalid Date"
If your date format (d
) is this,
如果您的日期格式 ( d
) 是这样的,
"dd-mm-yyyy"
do this,
做这个,
var a = d.split("-") ;
var date = new Date( a[2], (a[1] - 1), a[0] ) ;
this should net you a working Date
Object
. Or use the datepicker plug-in for jQuery as described in this post.
这应该会让你工作Date
Object
。或在此描述的使用日期选择器插件的jQuery岗位。
回答by Jukka K. Korpela
Instead of using built-in Date to string conversions, which are implementation-dependent, and instead of coding the logic yourself, it is best to use a suitable library. Here's one simple way of doing it using Globalize.js:
与其使用依赖于实现的内置 Date 到字符串的转换,也不要自己编写逻辑代码,最好使用合适的库。这是使用Globalize.js的一种简单方法:
function dayOfWeek(s) {
var d = Globalize.parseDate(s, 'yyyy/M/d');
if(d == null) {
return null;
} else {
return Globalize.format(d, 'dddd');
}
}
This will parse input in the format YYYY/MM/DD (with obvious modifications if your actual format is YYYY-MM-DD), more exactly so that the year is in 4 digits (yyyy) and the month (M) and day (d) may be in 1 or 2 digits (use MM and dd instead if you wish to enforce 2 digits). Then it writes the date so that only the day of the week is written, as a word (dddd). If the input is not of the specified format, the function returns null, and you should of course have some planned error handling for this case.
这将以 YYYY/MM/DD 格式解析输入(如果您的实际格式是 YYYY-MM-DD,则有明显的修改),更准确地说,年份是 4 位数字 (yyyy) 和月份 (M) 和日期 ( d) 可以是 1 位或 2 位数字(如果您希望强制使用 2 位数字,请改用 MM 和 dd)。然后它写入日期,以便只写入星期几,作为一个字 (dddd)。如果输入不是指定的格式,则该函数返回 null,您当然应该针对这种情况进行一些有计划的错误处理。
As an extra bonus, the code will be globalization-ready: if you ever need to modify the code to produce the names in another language, you just add one function invocation that sets the locale (language) for Globalize.js.
作为额外的好处,代码将支持全球化:如果您需要修改代码以生成另一种语言的名称,您只需添加一个函数调用来设置 Globalize.js 的区域设置(语言)。
回答by Gadde
回答by Snake Eyes
Regarding to new updated code:
关于新的更新代码:
$(function()
{
$("a").click(function () {
var dayNames = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var nData = new Date();
//alert(nData);
//alert(nData.getDay());
//var day = d.split("-")[2];
//var t = day.replace("0","");
//alert(t);
alert(dayNames[nData.getDay()]);
});
});
the above code is an example. Edit http://jsfiddle.net/qgDHB/with your needs.
上面的代码是一个例子。根据您的需要编辑http://jsfiddle.net/qgDHB/。
I removed some lines which are not needed.
我删除了一些不需要的行。
回答by Darren Cook
Inspired by the Globalize answer above, here is how to do it in the Sugar.jslibrary:
受上面 Globalize 答案的启发,以下是如何在Sugar.js库中执行此操作:
<script src="sugar.min.js"></script>
...
var test = Date.create(d);
console.log(test.format("{Weekday}"));
d
can be in loads of different formats (including string or secs-since-1970 or JS Date object) and the output format choices are equally comprehensive.
d
可以加载不同的格式(包括字符串或 secs-since-1970 或 JS Date 对象),并且输出格式选择同样全面。
Sugar.js is 17.9K minified and gzipped (which includes all the other features it adds, not just the date stuff); that is about twice the size of Moment.js, the alternative I was considering. But Moment.js does not have the equivalent of the {dow}
or {weekday}
formats.
Sugar.js 是 17.9K 压缩和 gzip(包括它添加的所有其他功能,而不仅仅是日期内容);这大约是我正在考虑的替代方案 Moment.js 的两倍。但是 Moment.js 没有{dow}
或{weekday}
格式的等价物。
回答by megakorre
Is the "format" you are talking about a regular string?
您所说的“格式”是常规字符串吗?
then you can do this
那么你可以这样做
var theDay = theDate.split("/")[2];