Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

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

like image 659
Hamster Avatar asked Dec 09 '10 20:12

Hamster


People also ask

How do I get today's date in JavaScript?

Use new Date() to generate a new Date object containing the current date and time. This will give you today's date in the format of mm/dd/yyyy. Simply change today = mm +'/'+ dd +'/'+ yyyy; to whatever format you wish.

What is new date () in JavaScript?

It is used to work with dates and times. The Date object is created by using new keyword, i.e. new Date(). The Date object can be used date and time in terms of millisecond precision within 100 million days before or after 1/1/1970.


2 Answers

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!)

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?

like image 51
Lekensteyn Avatar answered Oct 24 '22 12:10

Lekensteyn


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) 
like image 39
Parth Jasani Avatar answered Oct 24 '22 11:10

Parth Jasani