JavaScript Date - Exercises, Practice, Solution
This resource offers a total of 285 JavaScript Date Time problems for practice. It includes 57 main exercises, each accompanied by solutions, detailed explanations, and four related problems.
[An Editor is available at the bottom of the page to write and execute the scripts.]
JavaScript date object is used to create dates and times. Date objects are based on a time value that is the number of milliseconds since 1 January, 1970 UTC.
Constructor :
new Date(); new Date(value); new Date(dateString); new Date(year, month[, day[, hour[, minute[, second[, millisecond]]]]]);
1. Check Date Object
Write a JavaScript function to check whether an `input` is a date object or not.
Test Data :
console.log(is_date("October 13, 2014 11:13:00")); 
console.log(is_date(new Date(86400000))); 
console.log(is_date(new Date(99,5,24,11,33,30,0))); 
console.log(is_date([1, 2, 4, 0]));
Output :
false 
true 
true 
false
2. Get Current Date
Write a JavaScript function to get the current date.
Note : Pass a separator as an argument.
Test Data :
console.log(curday('/')); 
console.log(curday('-'));
Output :
"11/13/2014" 
"11-13-2014"
3. Days in Month
Write a JavaScript function to get the number of days in a month.
Test Data :
console.log(getDaysInMonth(1, 2012)); 
console.log(getDaysInMonth(2, 2012)); 
console.log(getDaysInMonth(9, 2012)); 
console.log(getDaysInMonth(12, 2012)); 
Output :
31 
29 
30 
31
4. Month Name from Date
Write a JavaScript function to get the month name from a particular date.
Test Data :
console.log(month_name(new Date("10/11/2009"))); 
console.log(month_name(new Date("11/13/2014")));
Output :
"October" 
"November"
5. Compare Dates
Write a JavaScript function to compare dates (i.e. greater than, less than or equal to).
Test Data :
console.log(compare_dates(new Date('11/14/2013 00:00'), new Date('11/14/2013 00:00'))); 
console.log(compare_dates(new Date('11/14/2013 00:01'), new Date('11/14/2013 00:00'))); 
console.log(compare_dates(new Date('11/14/2013 00:00'), new Date('11/14/2013 00:01')));
Output   :
"Date1 = Date2" 
"Date1 > Date2" 
"Date2 > Date1"
6. Add Minutes to Date
Write a JavaScript function to add specified minutes to a Date object.
Test Data :
console.log(add_minutes(new Date(2014,10,2), 30).toString());
Output :
"Sun Nov 02 2014 00:30:00 GMT+0530 (India Standard Time)"
7. Check Weekend
Write a JavaScript function to test whether a date is a weekend.
Note : Use standard Saturday/Sunday definition of a weekend.
Test Data :
console.log(is_weekend('Nov 15, 2014')); 
console.log(is_weekend('Nov 16, 2014')); 
console.log(is_weekend('Nov 17, 2014'));
Output :
"weekend" 
"weekend" 
undefined
8. Date Difference in Days
Write a JavaScript function to get the difference between two dates in days.
Test Data :
console.log(date_diff_indays('04/02/2014', '11/04/2014')); 
console.log(date_diff_indays('12/02/2014', '11/04/2014'));
Output :
216 
-28
9. Last Day of Month
Write a JavaScript function to get the last day of a month.
Test Data :
console.log(lastday(2014,0)); 
console.log(lastday(2014,1)); 
console.log(lastday(2014,11));
Output :
31 
28 
31
10. Yesterday's Date
Write a JavaScript function to calculate 'yesterday's day.
Test Data :
console.log(yesterday('Nov 15, 2014')); 
console.log(yesterday('Nov 16, 2015')); 
console.log(yesterday('Nov 17, 2016'));
Output :
"Fri Nov 14 2014 00:00:00 GMT+0530 (India Standard Time)" 
"Sun Nov 15 2015 00:00:00 GMT+0530 (India Standard Time)" 
"Wed Nov 16 2016 00:00:00 GMT+0530 (India Standard Time)"
11. Max Date in Array
Write a JavaScript function to get the maximum date from an array of dates.
Test Data :
console.log(max_date(['2015/02/01', '2015/02/02', '2015/01/03']));
Output :
 "2015/02/02"
12. Min Date in Array
Write a JavaScript function to get the minimum date from an array of dates.
Test Data :
console.log(min_date(['2015/02/01', '2015/02/02', '2015/01/03']));
Output :
"2015/01/03"
13. Minutes to Hours
Write a JavaScript function that returns the number of minutes in hours and minutes.
Test Data :
console.log(timeConvert(200));
Output :
"200 minutes = 3 hour(s) and 20 minute(s)."
14. Days in Year
Write a JavaScript function to get the number of days in a year.
Test Data :
   console.log(days_of_a_year(2015)); 
   365
   console.log(days_of_a_year(2016));
   366
15. Quarter of Year
Write a JavaScript function to get the quarter (1 to 4) of the year.
Test Data :
   console.log(quarter_of_the_year(new Date(2015, 1, 21)));  
   2
   console.log(quarter_of_the_year(new Date(2015, 10, 18)));
 4
16. Days Passed in Year
Write a JavaScript function to count the number of days passed since the year began.
Test Data :
   console.log(days_passed(new Date(2015, 0, 15))); 
 15
 console.log(days_passed(new Date(2015, 11, 14)));
 348
17. Unix to Time
Write a JavaScript function to convert a Unix timestamp to time.
Test Data :
   console.log(days_passed(new Date(2015, 0, 15))); 
   15
   console.log(days_passed(new Date(2015, 11, 14)));
   348
18. Calculate Age
18.
Write a JavaScript program to calculate age.
Test Data :
   console.log(calculate_age(new Date(1982, 11, 4))); 
   32
   console.log(calculate_age(new Date(1962, 1, 1)));
 53
19. Day of Month (2 Digits)
Write a JavaScript function to get the day of the month, 2 digits with leading zeros.  
   Test Data :
   d= new Date(2015, 10, 1); 
   console.log(day_of_the_month(d));
  "01"
20. Short Day Name
Write a JavaScript function to get a textual representation of a day (three letters, Mon through Sun).  
   Test Data :
   dt = new Date(2015, 10, 1); 
   console.log(short_Days(dt));
  "Sun"
21. Full Weekday Name
Write a JavaScript function to get a full textual representation of the weekday (Sunday through Saturday).  
   Test Data :
   dt = new Date(2015, 10, 1); 
   console.log(long_Days(dt));
  "Sunday"
22. ISO Weekday Number
Write a JavaScript function to get an ISO-8601 numeric representation of the day of the week (1 (for Monday) to 7 (for Sunday).  
   Test Data :
   dt = new Date(2015, 10, 1); 
   console.log(ISO_numeric_date(dt));
   7
23. Day Ordinal Suffix
Write a JavaScript function to get the English ordinal suffix for the day of the month, 2 characters (st, nd, rd, or th).  
   Test Data :
   dt = new Date(2015, 10, 1); 
   console.log(english_ordinal_suffix(dt));
  "1st"
24. ISO Week Number
Write a JavaScript function for obtaining ISO-8601 week numbers for weeks that begin on Monday.  
   Example : 42 (the 42nd week in the year)
   Test Data :
   dt = new Date(2015, 10, 1); 
   console.log(ISO8601_week_no(dt)); 
   44
25. Full Month Name
Write a JavaScript function to get a full text representation of a month, such as January or June.  
   Test Data :
   dt = new Date(2015, 10, 1);  
   console.log(full_month(dt));
  "November"
26. Numeric Month
Write a JavaScript function to get a numeric representation of a month, with leading zeros (01 through 12).  
   Test Data :
   dt = new Date(2015, 10, 1);  
   console.log(numeric_month(dt));
  "11"
27. Short Month Name
Write a JavaScript function to get a short textual representation of a month, three letters (Jan through Dec).  
   Test Data :
   dt = new Date(2015, 10, 1);
  
   console.log(short_months(dt));
  "Nov"
28. Full Year
Write a JavaScript function to get a full numeric representation of a year (4 digits).  
   Test Data :
   dt = new Date(2015, 10, 1);  
   console.log(full_year(dt)); 
   2015
29. Two-Digit Year
Write a JavaScript function to get a two-digit year representation.  
   Examples : 79 or 04
   Test Data :
   dt = new Date(1989, 10, 1); 
   console.log(sort_year(dt));
  "89"
30. Lowercase AM/PM
Write a JavaScript function to get lowercase Ante meridiem and Post meridiem.  
   Click me to see the solution
31. Uppercase AM/PM
Write a JavaScript function to get uppercase Ante meridiem and Post meridiem.
32. Swatch Internet Time
Write a JavaScript function to swatch Internet time (000 through 999).  
   Test Data :
   dt = new Date(1989, 10, 1);  
   console.log(internet_time(dt));
   812
33. 12-Hour Format
Write a JavaScript function to get the 12-hour format of an hour with leading zeros.  
   Test Data :
   dt = new Date(1989, 10, 1);  
   console.log(hours_with_zeroes(dt));   
  "12"
34. 24-Hour Format
Write a JavaScript function to get the 24-hour format of an hour without leading zeros.  
   Test Data :
   dt = new Date(1989, 10, 1);  
   console.log(hours_without_zeroes(dt));   
   0
35. Minutes Leading Zeros
Write a JavaScript function to get minutes with leading zeros (00 to 59).  
   Test Data :
   dt = new Date(1989, 10, 1);  
   console.log(minutes_with_leading_zeros(dt)); 
  "00"
36. Seconds Leading Zeros
Write a JavaScript function to get seconds with leading zeros (00 through 59).  
   Test Data :
   dt = new Date(1989, 10, 1);  
   console.log(seconds_with_leading_zeros(dt));   
  "00"
   
   Click me to see the solution
37. Get Timezone
Write a JavaScript function to get the Timezone.  
   Test Data :
   dt = new Date();  
   console.log(seconds_with_leading_zeros(dt));
  "India Standard Time"
   
   Click me to see the solution
38. Daylight Savings Check
Write a JavaScript function to find out whether or not the date is daylight savings time.  
   Test Data :
   dt = new Date();  
   console.log(daylights_savings(dt)); 
   1
39. GMT Difference
Write a JavaScript function to get the difference between Greenwich time (GMT) and in hours.  
   Test Data :
   dt = new Date();  
   console.log(diff_to_GMT(dt));   
  "+05.500"
40. Timezone Offset (Seconds)
Write a JavaScript function to get the timezone offset in seconds.  
    Note :  The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.
   Test Data :
   dt = new Date();  
   console.log(timezone_offset_in_seconds(dt));
   19800
41. Add Years to Date
Write a JavaScript function to add specified years to a  date. 
   Test Data :
   dt = new Date(2014,10,2); 
   console.log(add_years(dt, 10).toString());
   Output :
  "Sat Nov 02 2024 00:00:00 GMT+0530 (India Standard Time)"
42. Add Weeks to Date
Write a JavaScript function to add specified weeks to a  date. 
   Test Data :
   dt = new Date(2014,10,2); 
   console.log(add_weeks(dt, 10).toString());
   Output :
  "Sun Jan 11 2015 00:00:00 GMT+0530 (India Standard Time)"
43. Add Months to Date
Write a JavaScript function to add specified months to a  date. 
   Test Data :
   dt = new Date(2014,10,2);
 
   console.log(add_months(dt, 10).toString());
    Output :
  "Wed Sep 02 2015 00:00:00 GMT+0530 (India Standard Time)"
44. Time Diff in Minutes
Write a JavaScript function to get time differences in minutes between two dates. 
   Test Data :
   dt1 = new Date("October 13, 2014 11:11:00"); 
   dt2 = new Date("October 13, 2014 11:13:00"); 
   console.log(diff_minutes(dt1, dt2));   
   2
45. Time Diff in Hours
Write a JavaScript function to get time differences in hours between two dates. 
   Test Data :
   dt1 = new Date("October 13, 2014 08:11:00"); 
   dt2 = new Date("October 13, 2014 11:13:00"); 
   console.log(diff_hours(dt1, dt2)); 
 
   3
46. Time Diff in Days
Write a JavaScript function to get time differences in days between two dates. 
   Test Data :
   dt1 = new Date("October 13, 2014 08:11:00"); 
   dt2 = new Date("October 19, 2014 11:13:00"); 
   console.log(diff_days(dt1, dt2));
   6
  
  Click me to see the solution
47. Time Diff in Weeks
Write a JavaScript function to get time differences in weeks between two dates. 
   Test Data :
   dt1 = new Date("June 13, 2014 08:11:00"); 
   dt2 = new Date("October 19, 2014 11:13:00"); 
   console.log(diff_weeks(dt1, dt2));   
   18
48. Time Diff in Months
Write a JavaScript function to get time differences in months between two dates. 
   Test Data :
   dt1 = new Date("June 13, 2014 08:11:00"); 
   dt2 = new Date("October 19, 2014 11:13:00"); 
   console.log(diff_months(dt1, dt2));   
   5
49. Time Diff in Years
Write a JavaScript function to get time differences in years between two dates. 
   Test Data :
   dt1 = new Date("June 13, 2014 08:11:00"); 
   dt2 = new Date("October 19, 2017 11:13:00"); 
   console.log(diff_years(dt1, dt2));   
   3
50. Week Start Date
Write a JavaScript function to get the week's start date. 
   Click me to see the solution
51. Week End Date
Write a JavaScript function to get the week end date. 
     Click me to see the solution
52. Month Start Date
Write a JavaScript function to get the month's start date. 
   Click me to see the solution
53. Month End Date
Write a JavaScript function to get the month end date. 
   Click me to see the solution
54. Check Date in Range
Write a JavaScript function to check if a given date is between two other dates. 
   Click me to see the solution
55. Check Weekday or Weekend
Write a JavaScript program to check if the current date is a weekday or a weekend. 
   Click me to see the solution
56. Display Month Calendar
Write a JavaScript program that accepts a month in the format mm/yyyy and display the month's calendar. 
    
    Test Data: ('1/2019') ->
 
"M T W Th F S Su" " 1 2 3 4 5 6 " "7 8 9 10 11 12 13 " "14 15 16 17 18 19 20 " "21 22 23 24 25 26 27 " "28 29 30 31 "('2/1990') ->
"M T W Th F S Su" " 1 2 3 4 5 " "6 7 8 9 10 11 12 " "13 14 15 16 17 18 19 " "20 21 22 23 24 25 26 " "27 28 "Click me to see the solution
57. Day Name from Date String
Write a JavaScript program to get the name of a day based on a given date in string format. 
    
    Test Data:
  ("07/11/2000") -> "Tuesday"
  ("11/06/2017") -> "Sunday"
  ("11/26/2017") ->  "Not a valid Date!"
  Click me to see the solution
More to Come !
Live Demo
* To run the code mouse over on Result panel and click on 'RERUN' button.*
See the Pen javascript-common-editor by w3resource (@w3resource) on CodePen.
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
