0

A user suppose to enter date to get some analysis. The first question of the code requires him/her to enter date.
If a user enters a date that is some future date the code can not go on with future analysis and returns the message to a user You can not enter any future or date!, and return the process to the beginning requiring a user to enter date again.
If user otherwise enter proper date it returns the message: Ok!You can go on with process!.
The code is following:

var anydate = prompt('Enter date(any date before)', anydate)
var currentdate = new Date(); 
var datetime = "Last Sync: " + currentdate.getDate() + "/"
                + (currentdate.getMonth()+1)  + "/" 
                + currentdate.getFullYear() + " @ "  
                + currentdate.getHours() + ":"  
                + currentdate.getMinutes() + ":" 
                + currentdate.getSeconds();

console.log(datetime);

if (anydate>datetime){
    alert('You can not enter future date!');
}else{
    alert('OK!You can proceed with analysis!')
};

The first: any value I enter no matter if it is future or some former date, the code is returning me the second case Ok! You can go on with analysis!)?!

The second: if a user enters improper format which is not a date format, let's say some gibberish such as ljlkj;jkl;jk;kj;kj I would like the code to return him to the beggining and send the message Sorry this is not a date format!

Jérôme Teisseire
  • 1,518
  • 1
  • 16
  • 26
Ivan Vrzogic
  • 157
  • 1
  • 8
  • 1
    Have you tried using a while loop? Why not just wrap your code in a while loop? Maybe [this](https://stackoverflow.com/questions/37708465/how-to-create-while-loop-with-alert-and-prompt-to-avoid-empty-input) will help you. – costaparas Dec 17 '20 at 11:34
  • costaparas, No! – Ivan Vrzogic Dec 17 '20 at 11:35
  • @IvanVrzogic why not? – evolutionxbox Dec 17 '20 at 11:36
  • on your code `anydate` is a **Date** but `datetime` is a **String**, hard to compare...`datetime` should be a **Date** – Jérôme Teisseire Dec 17 '20 at 11:38
  • Yeah, datetime is string! However when i enter some future date let's say 25/12/2020 and test it 25/12/2020>17/12/2020 it returns me "true"! On the other side when i enter 15/12/2020 and test it 15/12/2020>17/12/2020, it returns me "false" which means that the code recognizes which value is greater?! – Ivan Vrzogic Dec 17 '20 at 11:55

2 Answers2

1

This code resolves problem and after a user enters either undesired format or improper future date it returns back to the beginning!

The code:

var flag = false;
while (!flag){
var anydate = prompt('Enter some date(any date before today) in format yyyy-mm-dd', anydate);
let today = new Date().toISOString().slice(0,10);
var d = new Date(today);
var e = new Date(anydate);
var Year = d.getFullYear().toString();
var Month = (d.getMonth()+1).toString();
var Date = d.getDate().toString();
var Year1 = e.getFullYear().toString();
var Month1 = (e.getMonth()+1).toString();
var Date1 = e.getDate().toString();
if((Year1>Year)||(Month1>Month)||(Date1>Date)||(Month1>12)||(Date1>31)){
alert('Your data is invalid! Enter valid date please!');
flag = false}else{
alert('OK! You can go on with process!')}
};
Ivan Vrzogic
  • 157
  • 1
  • 8
0

Just an answer for the first question, haven't worked a lot with date, so won't be able to help you with others.

var exit = false;
while(!exit){
    var anydate = prompt('Enter date(any date before)', anydate)
    var currentdate = new Date(); 
    var datetime = "Last Sync: " + currentdate.getDate() + "/"
                    + (currentdate.getMonth()+1)  + "/" 
                    + currentdate.getFullYear() + " @ "  
                    + currentdate.getHours() + ":"  
                    + currentdate.getMinutes() + ":" 
                    + currentdate.getSeconds();

    console.log(datetime);

    if (anydate>datetime){
        alert('You can not enter future date!');
    }else{
        exit = true;
        alert('OK!You can proceed with analysis!')
    };
}
Drunken Janna
  • 146
  • 1
  • 10
  • I've tried your code and still returns me the same as before! – Ivan Vrzogic Dec 17 '20 at 11:52
  • Like I said, it will only help you with the first part of the question, there is a problem with anydate>datetime for second part of question – Drunken Janna Dec 17 '20 at 12:41
  • Both anydate and datetime are string variables. To determine which value is greater the essential is to extract numerical values of respectively year, month and day and transform them to an integer format and then they will be ready for comparison which one is greater or less?! This suppose to be the key of this problem solving! – Ivan Vrzogic Dec 17 '20 at 13:04
  • 1
    Save the date data in different variables and strings with dates in different variables. Then just compare the variables with data without strings. – Drunken Janna Dec 17 '20 at 16:32
  • Basicaly the problem has almost been resolved except I miss something which would be equivalent to go to, in a case if condition is not met either improper format is entered, or improper date(future date) is entered to return back the code to the beginning or to the first line to required a user to enter proper date format and some past date! – Ivan Vrzogic Dec 18 '20 at 16:13
  • That's bascially what I included in my first answer, if the date was the future date, it would continue looping over. – Drunken Janna Dec 19 '20 at 20:00