Practice 50 JavaScript Datetime Programming Questions, TechnoVlogs

Practice 50 JavaScript Datetime Programming Questions


Q1. Write a JavaScript program to get the current date and time.  
Input: N/A  
Expected Output: Current date and time (e.g., 2024-12-31T12:45:00)

Q2. Write a JavaScript program to get the current date.  
Input: N/A  
Expected Output: Current date (e.g., 2024-12-31)

Q3. Write a JavaScript program to get the current time.  
Input: N/A  
Expected Output: Current time (e.g., 12:45:00)

Q4. Write a JavaScript program to get the day of the week from a given date.  
Input: date = "2024-12-31"  
Expected Output: Tuesday

Q5. Write a JavaScript program to convert a given date to a specific string format.  
Input: date = "2024-12-31"  
Expected Output: 31st December 2024

Q6. Write a JavaScript program to add days to a given date.  
Input: date = "2024-12-31", daysToAdd = 5  
Expected Output: 2025-01-05

Q7. Write a JavaScript program to subtract days from a given date.  
Input: date = "2024-12-31", daysToSubtract = 5  
Expected Output: 2024-12-26

Q8. Write a JavaScript program to calculate the number of days between two dates.  
Input: date1 = "2024-12-01", date2 = "2024-12-31"  
Expected Output: 30

Q9. Write a JavaScript program to check if a given year is a leap year.  
Input: year = 2024  
Expected Output: true

Q10. Write a JavaScript program to find the number of days in a given month.  
Input: month = 2, year = 2024  
Expected Output: 29

Q11. Write a JavaScript program to check if the current year is a leap year.  
Input: N/A  
Expected Output: true (if current year is a leap year)

Q12. Write a JavaScript program to get the name of the month from a given date.  
Input: date = "2024-12-31"  
Expected Output: December

Q13. Write a JavaScript program to get the number of weeks in the current year.  
Input: N/A  
Expected Output: 52 or 53 (depending on the year)

Q14. Write a JavaScript program to get the current timestamp in milliseconds.  
Input: N/A  
Expected Output: Timestamp in milliseconds (e.g., 1672551403000)

Q15. Write a JavaScript program to compare two dates.  
Input: date1 = "2024-12-31", date2 = "2025-01-01"  
Expected Output: false

Q16. Write a JavaScript program to get the number of seconds between two dates.  
Input: date1 = "2024-12-31T12:00:00", date2 = "2024-12-31T12:05:00"  
Expected Output: 300

Q17. Write a JavaScript program to get the first day of the current month.  
Input: N/A  
Expected Output: 2024-12-01

Q18. Write a JavaScript program to get the last day of the current month.  
Input: N/A  
Expected Output: 2024-12-31

Q19. Write a JavaScript program to find the difference in months between two dates.  
Input: date1 = "2024-01-01", date2 = "2024-12-31"  
Expected Output: 11

Q20. Write a JavaScript program to get the current day of the month.  
Input: N/A  
Expected Output: Current day (e.g., 31)

Q21. Write a JavaScript program to get the current year.  
Input: N/A  
Expected Output: 2024

Q22. Write a JavaScript program to check if a given date is valid.  
Input: date = "2024-12-31"  
Expected Output: true

Q23. Write a JavaScript program to get the difference in years between two dates.  
Input: date1 = "2020-01-01", date2 = "2024-12-31"  
Expected Output: 4

Q24. Write a JavaScript program to get the current weekday name.  
Input: N/A  
Expected Output: Current weekday (e.g., Tuesday)

Q25. Write a JavaScript program to format the current date in YYYY-MM-DD format.  
Input: N/A  
Expected Output: 2024-12-31

Q26. Write a JavaScript program to calculate the age of a person based on their birthdate.  
Input: birthdate = "1990-12-31"  
Expected Output: 34

Q27. Write a JavaScript program to find the day of the year from a given date.  
Input: date = "2024-12-31"  
Expected Output: 366 (if it's a leap year)

Q28. Write a JavaScript program to get the current time in HH:MM:SS format.  
Input: N/A  
Expected Output: 12:45:00

Q29. Write a JavaScript program to get the UTC date and time.  
Input: N/A  
Expected Output: 2024-12-31T12:45:00Z

Q30. Write a JavaScript program to subtract years from a given date.  
Input: date = "2024-12-31", yearsToSubtract = 2  
Expected Output: 2022-12-31

Q31. Write a JavaScript program to find the first day of the next month.  
Input: N/A  
Expected Output: 2025-01-01

Q32. Write a JavaScript program to find the last day of the previous month.  
Input: N/A  
Expected Output: 2024-11-30

Q33. Write a JavaScript program to check if two dates are the same.  
Input: date1 = "2024-12-31", date2 = "2024-12-31"  
Expected Output: true

Q34. Write a JavaScript program to find the current quarter of the year.  
Input: N/A  
Expected Output: Q4 (if the current date is in the fourth quarter)

Q35. Write a JavaScript program to get the number of days in a specific month of a year.  
Input: month = 2, year = 2024  
Expected Output: 29

Q36. Write a JavaScript program to get the current time in HH:MM:SS AM/PM format.  
Input: N/A  
Expected Output: 12:45:00 PM

Q37. Write a JavaScript program to add months to a given date.  
Input: date = "2024-12-31", monthsToAdd = 2  
Expected Output: 2025-02-28

Q38. Write a JavaScript program to subtract months from a given date.  
Input: date = "2024-12-31", monthsToSubtract = 2  
Expected Output: 2024-10-31

Q39. Write a JavaScript program to check if a given date is in the past.  
Input: date = "2024-12-31"  
Expected Output: false (if the current date is before 2024-12-31)

Q40. Write a JavaScript program to get the ISO 8601 formatted date.  
Input: N/A  
Expected Output: 2024-12-31T12:45:00.000Z

Q41. Write a JavaScript program to get the number of days in the current month.  
Input: N/A  
Expected Output: 31 (for December)

Q42. Write a JavaScript program to get the current date and time in MM/DD/YYYY HH:MM:SS format.  
Input: N/A  
Expected Output: 12/31/2024 12:45:00

Q43. Write a JavaScript program to find the first day of the previous month.  
Input: N/A  
Expected Output: 2024-11-01

Q44. Write a JavaScript program to find the current date without the time.  
Input: N/A  
Expected Output: 2024-12-31

Q45. Write a JavaScript program to compare two times.  
Input: time1 = "12:00:00", time2 = "12:30:00"  
Expected Output: false

Q46. Write a JavaScript program to convert a time string to a Date object.  
Input: timeString = "12:45:00"  
Expected Output: 2024-12-31T12:45:00.000Z

Q47. Write a JavaScript program to get the time difference in minutes between two times.  
Input: time1 = "12:00:00", time2 = "12:30:00"  
Expected Output: 30

Q48. Write a JavaScript program to find the last day of the next month.  
Input: N/A  
Expected Output: 2025-01-31

Q49. Write a JavaScript program to get the weekday name of a given date.  
Input: date = "2024-12-31"  
Expected Output: Tuesday

Q50. Write a JavaScript program to get the date after 30 days from the current date.  
Input: N/A  
Expected Output: 2025-01-30

Share on Social Media