This will give you the difference between two dates, in milliseconds
var diff = Math.abs(date1 - date2);
In your example, it’d be
var diff = Math.abs(new Date() - compareDate);
You need to make sure that compareDate
is a valid Date
object.
Something like this will probably work for you
var diff = Math.abs(new Date() - new Date(dateStr.replace(/-/g,'/')));
i.e. turning "2011-02-07 15:13:06"
into new Date('2011/02/07 15:13:06')
, which is a format the Date
constructor can comprehend.
Related Posts:
- How to format a JavaScript date
- How to format a JavaScript date
- How do I get the current date in JavaScript?
- Get String in YYYYMMDD format from JS date object?
- Compare two dates with JavaScript
- Compare dates with javascript
- Add days to JavaScript Date
- Parsing a string to a date in JavaScript
- Format JavaScript date as yyyy-mm-dd
- Add days to JavaScript Date
- How to validate date with format “mm/dd/yyyy” in JavaScript?
- Getting current date and time in JavaScript
- Shortest way to print current year in a website
- Validate date in dd/mm/yyyy format using JQuery Validate
- How to subtract days from a plain Date?
- How to add 30 minutes to a JavaScript Date object?
- How do you convert a JavaScript date to UTC?
- Convert UTC Epoch to local date
- Convert normal date to unix timestamp
- Check time difference in Javascript
- How to convert date to timestamp?
- How to format a UTC date as a `YYYY-MM-DD hh:mm:ss` string using NodeJS?
- Moment Js UTC to Local Time
- Get hours difference between two dates in Moment Js
- Javascript: getFullyear() is not a function
- Google reCAPTCHA V3.0 not working. “ERROR for site owner: Invalid key type”
- Javascript split regex question
- Find object by id in an array of JavaScript objects
- Cannot set property InnerHTML of null [duplicate]
- How can I submit a form using JavaScript?
- How fix ‘Failed to load resource: the server responded with a status of 403’?
- How do I empty an array in JavaScript?
- JavaScript TypeError: Cannot read property ‘style’ of null
- JavaScript string newline character?
- How to create a GUID / UUID
- How does “/\s/g” replace spaces with other characters?
- What is the correct way to check for string equality in JavaScript?
- Usage of the backtick character (`) in JavaScript
- Why is using “for…in” for array iteration a bad idea?
- Number converted in 1e+30
- Static variables in JavaScript
- ReferenceError : window is not defined at object.
Node.js - Node.js Port 3000 already in use but it actually isn’t?
- XML Parsing Error: no root element found Location in Console FF
- TypeError: app.use() requires middleware functions
- Possible to extend types in Typescript?
- JavaScript enumerator?
- “.addEventListener is not a function” why does this error occur?
- XMLHttpRequest status 0 (responseText is empty)
- What is the meaning of || in javascript?
- Coin toss with JavaScript and HTML
- Does JavaScript support array/list comprehensions like Python?
- Play an audio file using jQuery when a button is clicked
- Is there a simple way to create a javascript lookup table?
- Disable Chrome strict MIME type checking
- Why am I getting “ReferenceError: getElementById is not defined”?
- Can a for loop increment/decrement by more than one?
- jQuery equivalent of querySelector
- document.getElementById().value doesn’t set the value
- How to remove part of a string?
- How to run a function when the page is loaded?
- jQuery replace one class with another
- Clearing my form inputs after submission
- Scope Chain in Javascript
- Changing the image source using jQuery
- Reload an iframe with jQuery
- JavaScript pattern for multiple constructors
- Set CSS property in JavaScript?
- how to write javascript code inside php
- javascript convert int to float
- Uncaught TypeError: Cannot read property ‘toLowerCase’ of undefined
- How do I convert a JavaScript forEach loop/function to CoffeeScript
- ZIP Code (US Postal Code) validation
- Is there a standard function to check for null, undefined, or blank variables in JavaScript?
- Detecting a mobile browser
- jQuery .animate() not working
- how to add inside a text box?
- is it posible to add anime.js in WordPress
- Is it possible to enqueue the Youtube API script or does it have to be inline?
- What type of template are WP media-modal’s templates?
- is_front_page() not working
- Gutenberg RichText
- Sending custom data with Media Frame Model
- Unwanted line break before
- Multiple Conditional Controllers
- Adding javascript trigger to user role selection on the user-edit page
- Weird links found in wordpress footer
- Looping over wordpress meta to create “ ‘s?
- How to include a JSON file on my page?
- Adding inline JavaScript after wp_enqueue_scripts
- Install GTM in pure javascript through functions.php
- Update media library attachments
- JavaScript Libraries in WordPress
- how to let users upload their custom cover image
- how to use nimble-API and Display data?
- Create new product with woocommerce REST API with javascript (clientside)?
- Combining results from WP-API using AngularJS
- Get current day using javascript [closed]
- dropdown does not work [closed]
- Anyway to disable the auto excerpt creation?