• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Archives for Time & Date

Time & Date

How To Get a Timestamp in JavaScript

How To Get a Timestamp in JavaScript

Here are a few examples of how to get the timestamp in JavaScript.

Time & Date

Date difference in months

Date difference in months

Calculates the difference (in months) between two dates. Use Date.prototype.getFullYear() and Date.prototype.getMonth() to calculate the difference (in months) between two Date objects. Source https://www.30secondsofcode.org/js/s/get-months-diff-between-dates

Time & Date

Unix timestamp from date

Unix timestamp from date

Gets the Unix timestamp from a Date object. Use Date.prototype.getTime() to get the timestamp in milliseconds and divide by 1000 to get the timestamp in seconds. Use Math.floor() to appropriately round the resulting timestamp to an integer. Omit the argument, date, to use the current date. Source https://www.30secondsofcode.org/js/s/get-timestamp

Time & Date

Date of yesterday

Date of yesterday

Results in a string representation of yesterday’s date. Use the Date constructor to get the current date. Decrement it by one using Date.prototype.getDate() and set the value to the result using Date.prototype.setDate(). Use Date.prototype.toISOString() to return a string in yyyy-mm-dd format. Source https://www.30secondsofcode.org/js/s/yesterday

Time & Date

Calculate the difference (in hours) between two dates

Calculate the difference (in hours) between two dates

Subtract the two Date objects and divide by the number of milliseconds in an hour to get the difference (in hours) between them. Source https://www.30secondsofcode.org/js/s/get-hours-diff-between-dates

Time & Date

Check if a date is the same as another date

Check if a date is the same as another date

Use Date.prototype.toISOString() and strict equality checking (===) to check if the first date is the same as the second one.

Time & Date

Check if the given year is a leap year

Check if the given year is a leap year

Use new Date(), setting the date to February 29th of the given year. Use Date.prototype.getMonth() to check if the month is equal to 1.

Time & Date

Check if the given date is a weekend

Check if the given date is a weekend

Use Date.prototype.getDay() to check weekend by using a modulo operator (%).

Time & Date

How to Add and Subtract Time From a Date in JavaScript

How to Add and Subtract Time From a Date in JavaScript

How you can add time to a Date object and subtract time from a Date object in JavaScript.

Time & Date

Get a formatted list of months

Get a formatted list of months

Here’s a trick for getting back a formatted month name (like January or Jan).

Time & Date

  • Go to page 1
  • Go to page 2
  • Go to Next Page »

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2023 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us