给定特定的日期或时间戳,提取特定月份的星期几和星期几 [英] Extract day and week number of a specific month given a specific date or time stamp

查看:397
本文介绍了给定特定的日期或时间戳,提取特定月份的星期几和星期几的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果可能的话,最好在jQuery中使用,否则任何Javascript都可以使用.

Preferred in jQuery, if possible, otherwise any Javascript will work.

我想获取给定特定日期或时间戳记的月份的星期几.

I'd like to get the day and week number of month given a specific date or timestamp.

例如

2010-10-26将返回星期二和4作为星期数

2010-10-26 would return Tuesday and 4 as the week number

2010-11-11将返回星期四和2作为星期数

2010-11-11 would return Thursday and 2 as the week number

推荐答案

您需要查看 JavaScript日期对象.

要获取星期几,您需要执行以下操作:

To get the day of the week, you need to do something like the following:

var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];

var myDate = new Date('2010-10-26');

var dayOfWeek = days[myDate.getDay()]; // dayOfWeek == 'Tuesday'

这是因为getDay()返回的是数字0-6,而不是字符串.这很有用,例如进行国际化.

This is because getDay() returns a number 0-6 rather than a string. This is useful e.g. for internationalisation.

您的其他要求(一个月中的第几周)将需要自定义编码-使用本地JS日期对象无法完成.

Your other requirement (week of month) would need custom coding -- it can't be done using the native JS date object.

这篇关于给定特定的日期或时间戳,提取特定月份的星期几和星期几的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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