Javascript to compare two dates, from strings, begin = end

2024/2/27 8:50:09

I get two strings formated like (Brazilian Format): "DD/MM/YYYY", I need to compare both. Since the first field is the begin and the last is the end,

My validation is begin <= end

Date.new(begin) is generating 'invalid date' even on ISO !

Answer

Don't use Date.new. Use new Date(). Because of the format of your date string, I would recommend grabbing each field individually and passing them into the constructor:

var startYear = parseInt(document.getElementById('startYear'), 10);
var startMonth = parseInt(document.getElementById('startMonth'), 10) - 1; // as per Residuum's comment
var startDay = parseInt(document.getElementById('startDay'), 10);
var start = new Date(startYear, startMonth, startDay);

etc. If you're handed a date string, then you can use fuzzy lollipop's method to get each field from the string. I'm not sure if the Date constructor will accept unparsed strings for the individual fields, however.

The, once you have the two dates you'd like to compare, just compare their values in milliseconds since the epoch:

function isValid(start, end) {return start.getTime() < end.getTime();
}
http://en.ppmy.cn/q/41551.html

Related Q&A

Remove leading zeroes in datestring

I have a date string like the following: 2011-02-03. I want to remove the leading zeroes in the day and month part of the string. How do I do this?

Clone form and increment ID

Consider the following form:<form><input type="button" value="Input Button"/><input type="checkbox" /><input type="file" id="file"…

Data URI to Object URL with createObjectURL in chrome/ff

I have base64 string of an image. How can I convert this to Object URL? The purpose is to try and see if my svg-editor will be faster, by injecting Blob URL to the DOM instead of a very large base64 s…

How to center div?

I have a problem with centering div in HTML (vertical & horizontal). My code looks something like this:<div id="container">SOME HTML</div>#container{width: 366px;height: 274px…

Multithreading with javascript promises

Just learning promises. Javascript is single threaded right? So when it uses fetch api to make http requests it all happens in one thread? How does it manage concurrency with PromisePool then? var p…

jQuery UI Slider - with mousewheel support?

as you may already know Im new to jQuery, so Code-Improvements not belonging to this theme are still very allowed.This is my HTML-Code:<div style="display: inline-block; width: 120px;">…

Do Firebase Cloud Functions callable functions enforce end user authentication?

I would like to know if, by itself, callables functions with Firebase Cloud Functions, for example : exports.myCallableFunction = functions.https.onCall((data, context) => { //... });are safe by th…

javascript 5 random non duplicating integers from 0 - 20

Whats the best way to generate 5 random non duplicating integers from 0 - 20?Im thinking, use Math.random with floor, loop it 5 times, check for duplicates, if duplicate, random again.Whats your way?

JSON Scraping - Convert military time to standard time via Javascript

I am scraping JSON data from a url. The time is military time and I was wondering if there is a way once I retrieve on the client side to convert it to standard time.Here is the JSON:[{SaturdayClose: &…

javascript hoisting: what would be hoisted first — variable or function?

Recently I was confused about javascript hoisting behavior and now I got stuck with that.So, there are two examples.var alpha = alpha;var beta = beta;f(); //betavar f = function f1() {console.log(beta)…