2016-09-21 3 views
-2

Ich wurde versucht, am nächsten Tag zu finden mit Javascript funktioniert es für statische Datum perfekt, aber nicht für dynamische Datum.Wie finden Sie am nächsten Tag mit Javascript

Auch für das Datum zwischen 1 bis 12 arbeiten und geben Sie mir für sie perfekte Leistung, nicht für hohe Datum arbeiten dann 12

Bitte helfen Sie mir, dieses Problem zu lösen!

<script type="text/javascript"> 
 
function next_day() { \t 
 
\t // var date = document.getElementById('date').value; 
 
\t var textbox_Value = document.getElementById('date').value; 
 
\t console.log("input date : " + textbox_Value); 
 
\t var fDate = new Date(textbox_Value.replace(/(\d{2})-(\d{2})-(\d{4})/, "$2/$1/$3")); //change date formate mm/dd/yyyy to dd/mm/yyyy 
 
\t var dayWithSlashes = (fDate.getDate()) + '/' + (fDate.getMonth()+1) + '/' + fDate.getFullYear(); 
 
\t console.log("Foramat changed date : " + dayWithSlashes); 
 
\t var day_in_js_format = new Date(dayWithSlashes); 
 
\t console.log("convert in date format : " + day_in_js_format); 
 
\t day_in_js_format.setDate(day_in_js_format.getDate() + 1); 
 
\t console.log("day_in_js_format++ : " + day_in_js_format); 
 
\t var final_Result = (day_in_js_format.getDate()) + '/' + (day_in_js_format.getMonth()+1) + '/' + day_in_js_format.getFullYear(); 
 
\t console.log("next day : " + final_Result); 
 
} 
 
</script>
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
\t <title> 
 
\t \t Next day using javascript 
 
\t </title> 
 
\t <!-- Latest compiled and minified CSS & JS --> 
 
\t <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> 
 
</head> 
 
<body> 
 

 
\t <div class="container fix-top"> 
 
\t \t <form action="" method="POST" role="form"> 
 
\t \t \t <legend>Form title</legend> 
 
\t \t 
 
\t \t \t <div class="form-group"> 
 
\t \t \t \t <label for="">Next day</label> 
 
\t \t \t \t <input type="text" class="form-control" id="date" name="date" placeholder="Input field"> 
 
\t \t \t </div> \t \t \t 
 
\t \t 
 
\t \t \t <button type="button" onclick="next_day();" class="btn btn-primary">Submit</button> 
 
\t \t </form> 
 
\t </div> 
 
\t 
 
\t <script src="//code.jquery.com/jquery.js"></script> 
 
\t <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> 
 
</body> 
 
</html>

+1

Symptons sagen, dass Sie Monate und Daten bei der Erstellung eines Datums gemischt haben ... – Teemu

+1

Verwenden Sie nicht ein nicht-Standard TT/MM/JJJJ-Format beim Erstellen des Datumsobjekts. Verwenden Sie stattdessen JJJJ-MM-TT. – JJJ

+0

Ich habe nicht Monate und Termine gemischt Sie bitte überprüfen Sie Ihr Protokoll zum besseren Verständnis – HirenMangukiya

Antwort

0

Funktion Tagen zu einem Datum hinzuzufügen:

function addDays(dtRef, intDays) { 
     if (!(typeof dtRef == "object" 
      && typeof dtRef['setDate'] == "function" 
      && typeof dtRef['getDate'] == "function")) { 
      dtRef = new Date(); 
     } 
     if (typeof intDays != "number") { 
      intDays = 1; 
     } 
     dtRef.setDate(dtRef.getDate() + intDays); 
     return dtRef; 
    }; 

Sie diese Funktion ohne Parameter aufrufen können, und es wird das aktuelle Datum mit einem Tag der +1 Offsets . Oder Sie können das Datum und den Offset in Tagen eingeben und es wird das neue Datum zurückgegeben.

+0

Danke sir, aber es funktioniert nicht für Format TT/MM/JJJJ seine Arbeit als mm/TT/JJJJ – HirenMangukiya

+0

Sie müssen die Funktion ein gültiges Javascript-Datum liefern, die Funktion gibt ein Datum Objekt, das korrekt sein wird und Sie erhalten können alles was du davon brauchst. – SPlatten

Verwandte Themen