【问题标题】:Javascript, Time and Date: Getting the current minute, hour, day, week, month, year of a given millisecond timeJavascript,时间和日期:获取给定毫秒时间的当前分钟、小时、日、周、月、年
【发布时间】:2011-05-23 02:56:11
【问题描述】:

我还在纠结这个图书馆,但我没时间了,所以我就跳到剧透部分问问。使用给定的任意毫秒时间值(例如您从.getTime() 提供的那种),我如何获取当前分钟、小时、日、月中的周、月、年中的周和年具体的毫秒时间?

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

【问题讨论】:

  • 规范中都有解释。有一节描述了日期方法,甚至是抽象算法。

标签: javascript datetime


【解决方案1】:

变量名应该是描述性的:

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();

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

接上一段代码:

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;

没有直接的方法来获取一年中的一周。有关该问题的答案,请参阅Is there a way in javascript to create a date object using year & ISO week number?

【讨论】:

  • 这不是确定闰年的完整算法。
  • 查看维基百科后对其进行了编辑。感谢您的关注。
  • 这个想法是对的,但我认为逻辑有点不对劲......也许year%400===0 || (year%4===0 && year%100!==0)
  • 再次感谢 bobince。我现在正在使用 Peter Bailey 的方法。好点子。我总是忘记这种可能性。
【解决方案2】:

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

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)

【讨论】:

  • 在从中提取值之前,您应该始终获取日期的本地副本,因为每个 newDate() 返回的日期/时间可能会略有不同,您最终可能会遇到意外结果。
【解决方案3】:

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

分、时、日等:

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"));

【讨论】:

  • 要检查闰年,您必须检查if (((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))。这是公历的定义。
【解决方案4】:

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

除了自己计算(因此必须正确计算闰年)之外,您还可以使用日期计算来进行:

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

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

【讨论】:

  • UTC 即使在 12 月过后(12 日及以上)仍然有效?
  • 是的,它适用于 UTC 或本地时间。 (好吧,除非在当地时间 1 月 1 日发生时区更改,在这种情况下,当地时间可能会出错。我不知道任何地方曾经发生过这种情况,但我学会了总是对本地时区规则会产生奇怪的东西......)
猜你喜欢
  • 2021-06-06
  • 2011-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多