how to compare current date to a future date and validate it in javascript
`
`
I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is 2 months greater than the current date. If not, I'd like to display a message to notifying the user the date is less then 2 months but user can still proceed with filling the form after the notification.
Below is the form i want to add the function to.
javascript validation date
add a comment |
`
`
I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is 2 months greater than the current date. If not, I'd like to display a message to notifying the user the date is less then 2 months but user can still proceed with filling the form after the notification.
Below is the form i want to add the function to.
javascript validation date
I recommend you to use the momentjs.com library, try it. It makes manipulation of dates easy
– Marcos Pérez Gude
Dec 3 '15 at 16:03
You should check stackoverflow.com/questions/2706125/…
– Daniel Corzo
Dec 3 '15 at 16:18
add a comment |
`
`
I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is 2 months greater than the current date. If not, I'd like to display a message to notifying the user the date is less then 2 months but user can still proceed with filling the form after the notification.
Below is the form i want to add the function to.
javascript validation date
`
`
I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is 2 months greater than the current date. If not, I'd like to display a message to notifying the user the date is less then 2 months but user can still proceed with filling the form after the notification.
Below is the form i want to add the function to.
javascript validation date
javascript validation date
edited Dec 3 '15 at 17:25
iyke
asked Dec 3 '15 at 16:02
iykeiyke
113
113
I recommend you to use the momentjs.com library, try it. It makes manipulation of dates easy
– Marcos Pérez Gude
Dec 3 '15 at 16:03
You should check stackoverflow.com/questions/2706125/…
– Daniel Corzo
Dec 3 '15 at 16:18
add a comment |
I recommend you to use the momentjs.com library, try it. It makes manipulation of dates easy
– Marcos Pérez Gude
Dec 3 '15 at 16:03
You should check stackoverflow.com/questions/2706125/…
– Daniel Corzo
Dec 3 '15 at 16:18
I recommend you to use the momentjs.com library, try it. It makes manipulation of dates easy
– Marcos Pérez Gude
Dec 3 '15 at 16:03
I recommend you to use the momentjs.com library, try it. It makes manipulation of dates easy
– Marcos Pérez Gude
Dec 3 '15 at 16:03
You should check stackoverflow.com/questions/2706125/…
– Daniel Corzo
Dec 3 '15 at 16:18
You should check stackoverflow.com/questions/2706125/…
– Daniel Corzo
Dec 3 '15 at 16:18
add a comment |
3 Answers
3
active
oldest
votes
If you used javascript then lets try to this one, It may help you.
<script type="text/javascript">
var date = new Date();
var month = date.getMonth()+1;
var day = date.getDay();
var year = date.getYear();
var newdate = new Date(year,month,day);
mydate=new Date('2011-04-11');
console.log(newdate);
console.log(mydate)
if(newdate > mydate)
{
alert("greater");
}
else
{
alert("smaller")
}
</script>
Thanks for your help. Below is the code where i wish to add that script. Can you help me to modify it using the values i have in it
– iyke
Dec 3 '15 at 16:29
add a comment |
If your date is in mm/dd/yyyy
format in string then you can use the following method. It will return true if date is 2 months greater than the current date and false otherwise -
// dateString in "mm/dd/yyyy" string format
function checkMonth(dateString)
{
var enteredMS = new Date(dateString).getTime();
var currentMS = new Date().getTime();
var twoMonthMS = new Date(new Date().setMonth(new Date().getMonth() + 2)).getTime();
if(enteredMS >= twoMonthMS)
{
return true;
}
return false;
}
Invoke this as checkMonth("03/12/2016");
add a comment |
var d1 = new Date();
var d2 = new Date(d1);
console.log(d1 == d2); // prints false (wrong!)
console.log(d1 === d2); // prints false (wrong!)
console.log(d1 != d2); // prints true (wrong!)
console.log(d1 !== d2); // prints true (wrong!)
console.log(d1.getTime() === d2.getTime()); // prints true (correct)
Also you can do <,>,>=,<= etc
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34070628%2fhow-to-compare-current-date-to-a-future-date-and-validate-it-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you used javascript then lets try to this one, It may help you.
<script type="text/javascript">
var date = new Date();
var month = date.getMonth()+1;
var day = date.getDay();
var year = date.getYear();
var newdate = new Date(year,month,day);
mydate=new Date('2011-04-11');
console.log(newdate);
console.log(mydate)
if(newdate > mydate)
{
alert("greater");
}
else
{
alert("smaller")
}
</script>
Thanks for your help. Below is the code where i wish to add that script. Can you help me to modify it using the values i have in it
– iyke
Dec 3 '15 at 16:29
add a comment |
If you used javascript then lets try to this one, It may help you.
<script type="text/javascript">
var date = new Date();
var month = date.getMonth()+1;
var day = date.getDay();
var year = date.getYear();
var newdate = new Date(year,month,day);
mydate=new Date('2011-04-11');
console.log(newdate);
console.log(mydate)
if(newdate > mydate)
{
alert("greater");
}
else
{
alert("smaller")
}
</script>
Thanks for your help. Below is the code where i wish to add that script. Can you help me to modify it using the values i have in it
– iyke
Dec 3 '15 at 16:29
add a comment |
If you used javascript then lets try to this one, It may help you.
<script type="text/javascript">
var date = new Date();
var month = date.getMonth()+1;
var day = date.getDay();
var year = date.getYear();
var newdate = new Date(year,month,day);
mydate=new Date('2011-04-11');
console.log(newdate);
console.log(mydate)
if(newdate > mydate)
{
alert("greater");
}
else
{
alert("smaller")
}
</script>
If you used javascript then lets try to this one, It may help you.
<script type="text/javascript">
var date = new Date();
var month = date.getMonth()+1;
var day = date.getDay();
var year = date.getYear();
var newdate = new Date(year,month,day);
mydate=new Date('2011-04-11');
console.log(newdate);
console.log(mydate)
if(newdate > mydate)
{
alert("greater");
}
else
{
alert("smaller")
}
</script>
answered Dec 3 '15 at 16:14
Afroza YasminAfroza Yasmin
3621320
3621320
Thanks for your help. Below is the code where i wish to add that script. Can you help me to modify it using the values i have in it
– iyke
Dec 3 '15 at 16:29
add a comment |
Thanks for your help. Below is the code where i wish to add that script. Can you help me to modify it using the values i have in it
– iyke
Dec 3 '15 at 16:29
Thanks for your help. Below is the code where i wish to add that script. Can you help me to modify it using the values i have in it
– iyke
Dec 3 '15 at 16:29
Thanks for your help. Below is the code where i wish to add that script. Can you help me to modify it using the values i have in it
– iyke
Dec 3 '15 at 16:29
add a comment |
If your date is in mm/dd/yyyy
format in string then you can use the following method. It will return true if date is 2 months greater than the current date and false otherwise -
// dateString in "mm/dd/yyyy" string format
function checkMonth(dateString)
{
var enteredMS = new Date(dateString).getTime();
var currentMS = new Date().getTime();
var twoMonthMS = new Date(new Date().setMonth(new Date().getMonth() + 2)).getTime();
if(enteredMS >= twoMonthMS)
{
return true;
}
return false;
}
Invoke this as checkMonth("03/12/2016");
add a comment |
If your date is in mm/dd/yyyy
format in string then you can use the following method. It will return true if date is 2 months greater than the current date and false otherwise -
// dateString in "mm/dd/yyyy" string format
function checkMonth(dateString)
{
var enteredMS = new Date(dateString).getTime();
var currentMS = new Date().getTime();
var twoMonthMS = new Date(new Date().setMonth(new Date().getMonth() + 2)).getTime();
if(enteredMS >= twoMonthMS)
{
return true;
}
return false;
}
Invoke this as checkMonth("03/12/2016");
add a comment |
If your date is in mm/dd/yyyy
format in string then you can use the following method. It will return true if date is 2 months greater than the current date and false otherwise -
// dateString in "mm/dd/yyyy" string format
function checkMonth(dateString)
{
var enteredMS = new Date(dateString).getTime();
var currentMS = new Date().getTime();
var twoMonthMS = new Date(new Date().setMonth(new Date().getMonth() + 2)).getTime();
if(enteredMS >= twoMonthMS)
{
return true;
}
return false;
}
Invoke this as checkMonth("03/12/2016");
If your date is in mm/dd/yyyy
format in string then you can use the following method. It will return true if date is 2 months greater than the current date and false otherwise -
// dateString in "mm/dd/yyyy" string format
function checkMonth(dateString)
{
var enteredMS = new Date(dateString).getTime();
var currentMS = new Date().getTime();
var twoMonthMS = new Date(new Date().setMonth(new Date().getMonth() + 2)).getTime();
if(enteredMS >= twoMonthMS)
{
return true;
}
return false;
}
Invoke this as checkMonth("03/12/2016");
edited Dec 3 '15 at 16:23
answered Dec 3 '15 at 16:17
ChandanChandan
1,028710
1,028710
add a comment |
add a comment |
var d1 = new Date();
var d2 = new Date(d1);
console.log(d1 == d2); // prints false (wrong!)
console.log(d1 === d2); // prints false (wrong!)
console.log(d1 != d2); // prints true (wrong!)
console.log(d1 !== d2); // prints true (wrong!)
console.log(d1.getTime() === d2.getTime()); // prints true (correct)
Also you can do <,>,>=,<= etc
add a comment |
var d1 = new Date();
var d2 = new Date(d1);
console.log(d1 == d2); // prints false (wrong!)
console.log(d1 === d2); // prints false (wrong!)
console.log(d1 != d2); // prints true (wrong!)
console.log(d1 !== d2); // prints true (wrong!)
console.log(d1.getTime() === d2.getTime()); // prints true (correct)
Also you can do <,>,>=,<= etc
add a comment |
var d1 = new Date();
var d2 = new Date(d1);
console.log(d1 == d2); // prints false (wrong!)
console.log(d1 === d2); // prints false (wrong!)
console.log(d1 != d2); // prints true (wrong!)
console.log(d1 !== d2); // prints true (wrong!)
console.log(d1.getTime() === d2.getTime()); // prints true (correct)
Also you can do <,>,>=,<= etc
var d1 = new Date();
var d2 = new Date(d1);
console.log(d1 == d2); // prints false (wrong!)
console.log(d1 === d2); // prints false (wrong!)
console.log(d1 != d2); // prints true (wrong!)
console.log(d1 !== d2); // prints true (wrong!)
console.log(d1.getTime() === d2.getTime()); // prints true (correct)
Also you can do <,>,>=,<= etc
var d1 = new Date();
var d2 = new Date(d1);
console.log(d1 == d2); // prints false (wrong!)
console.log(d1 === d2); // prints false (wrong!)
console.log(d1 != d2); // prints true (wrong!)
console.log(d1 !== d2); // prints true (wrong!)
console.log(d1.getTime() === d2.getTime()); // prints true (correct)
var d1 = new Date();
var d2 = new Date(d1);
console.log(d1 == d2); // prints false (wrong!)
console.log(d1 === d2); // prints false (wrong!)
console.log(d1 != d2); // prints true (wrong!)
console.log(d1 !== d2); // prints true (wrong!)
console.log(d1.getTime() === d2.getTime()); // prints true (correct)
answered Dec 3 '15 at 17:40
Basilin JoeBasilin Joe
397620
397620
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34070628%2fhow-to-compare-current-date-to-a-future-date-and-validate-it-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I recommend you to use the momentjs.com library, try it. It makes manipulation of dates easy
– Marcos Pérez Gude
Dec 3 '15 at 16:03
You should check stackoverflow.com/questions/2706125/…
– Daniel Corzo
Dec 3 '15 at 16:18