Javascript、时间和日期:获取给定毫秒时间的当前分钟、小时、日、周、月、年

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4402934/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:16:55  来源:igfitidea点击:

Javascript, Time and Date: Getting the current minute, hour, day, week, month, year of a given millisecond time

javascriptdatetime

提问by Hamster

I'm still wrapping my head around this library, but I'm out of time so I'll just skip to the spoiler section and ask. With a given, arbitrary millisecond time value (like the kind you'd gave from .getTime()), how do I get the current minute, hour, day, week of the month, month, week of the year, and year of that specific millisecond of time?

我仍然在思考这个图书馆,但我没时间了,所以我会跳到剧透部分问问。对于给定的,任意的毫秒时间值(就像你在从得到的那种.getTime())的一年,如何获取当前分钟,小时,日,周月,月,周,和特定毫秒年时间?

Additionally, how do I retrieve the number of days of a given month? Anything I should know about regarding leap years and other stuff?

另外,如何检索给定月份的天数?关于闰年和其他东西,我应该知道什么?

回答by Lekensteyn

The variable names should be descriptive:

变量名称应该是描述性的:

var date = new Date;
date.setTime(result_from_Date_getTime);

var seconds = date.getSeconds();
var minutes = date.getMinutes();
var hour = date.getHours();

var year = date.getFullYear();
var month = date.getMonth(); // beware: January = 0; February = 1, etc.
var day = date.getDate();

var dayOfWeek = date.getDay(); // Sunday = 0, Monday = 1, etc.
var milliSeconds = date.getMilliseconds();

The days of a given month do not change. In a leap year, February has 29 days. Inspired by http://www.javascriptkata.com/2007/05/24/how-to-know-if-its-a-leap-year/(thanks Peter Bailey!)

给定月份的天数不会改变。在闰年,二月有 29 天。灵感来自http://www.javascriptkata.com/2007/05/24/how-to-know-if-its-a-leap-year/(感谢 Peter Bailey!)

Continued from the previous code:

接上前面的代码:

var days_in_months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// for leap years, February has 29 days. Check whether
// February, the 29th exists for the given year
if( (new Date(year, 1, 29)).getDate() == 29 ) days_in_month[1] = 29;

There is no straightforward way to get the week of a year. For the answer on that question, see Is there a way in javascript to create a date object using year & ISO week number?

没有直接的方法来获得一年中的一周。有关该问题的答案,请参阅JavaScript 中是否可以使用年份和 ISO 周数创建日期对象?

回答by Parth Jasani

Here is another method to get date

这是获取日期的另一种方法

new Date().getDate()          // Get the day as a number (1-31)
new Date().getDay()           // Get the weekday as a number (0-6)
new Date().getFullYear()      // Get the four digit year (yyyy)
new Date().getHours()         // Get the hour (0-23)
new Date().getMilliseconds()  // Get the milliseconds (0-999)
new Date().getMinutes()       // Get the minutes (0-59)
new Date().getMonth()         // Get the month (0-11)
new Date().getSeconds()       // Get the seconds (0-59)
new Date().getTime()          // Get the time (milliseconds since January 1, 1970)

回答by Shadow Wizard is Ear For You

Regarding number of days in month just use static switch command and check if (year % 4 == 0)in which case February will have 29 days.

关于一个月中的天数,只需使用静态 switch 命令并检查if (year % 4 == 0)在这种情况下二月将有 29 天。

Minute, hour, day etc:

分钟、小时、天等:

var someMillisecondValue = 511111222127;
var date = new Date(someMillisecondValue);
var minute = date.getMinutes();
var hour = date.getHours();
var day = date.getDate();
var month = date.getMonth();
var year = date.getFullYear();
alert([minute, hour, day, month, year].join("\n"));

回答by bobince

Additionally, how do I retrieve the number of days of a given month?

另外,如何检索给定月份的天数?

Aside from calculating it yourself (and consequently having to get leap years right), you can use a Date calculation to do it:

除了自己计算(因此必须正确计算闰年),您还可以使用 Date 计算来执行此操作:

var y= 2010, m= 11;            // December 2010 - trap: months are 0-based in JS

var next= Date.UTC(y, m+1);    // timestamp of beginning of following month
var end= new Date(next-1);     // date for last second of this month
var lastday= end.getUTCDate(); // 31

In general for timestamp/date calculations I'd recommend using the UTC-based methods of Date, like getUTCSecondsinstead of getSeconds(), and Date.UTCto get a timestamp from a UTC date, rather than new Date(y, m), so you don't have to worry about the possibility of weird time discontinuities where timezone rules change.

一般来说,对于时间戳/日期计算,我建议使用基于 UTC 的 Date 方法,例如getUTCSeconds代替getSeconds(),并Date.UTC从 UTC 日期获取时间戳,而不是new Date(y, m),因此您不必担心出现奇怪的可能性时区规则发生变化的时间不连续性。