This question already has answers here:
How to set input type date's default value to today?
(43 answers)
Closed 4 years ago.
I want to show current date on input field date.My code is down below
HTML:
<div class=\"col-lg-4\">
<label>Date</label>
<input type=\"date\" name=\"dateto1\" id=\"dateto1\" class=\"form-control\">
</div>
JAVASCRIPT:
<script type="text/javascript">
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd = '0'+dd
}
if(mm<10) {
mm = '0'+mm
}
// today = yyyy + '/' + mm + '/' + dd;
today = mm + '/' + dd + '/' + yyyy;
console.log(today);
document.getElementById('dateto1').value = today;
}
window.onload = function() {
getDates();
};
</script>
this code is working fine if i use input type "text" but for type date its not working.Please guide me to solve this issue.
Thanks
You should use the yyyy-MM-dd format:
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd = '0'+dd
}
if(mm<10) {
mm = '0'+mm
}
// today = yyyy + '/' + mm + '/' + dd;
today = yyyy + '-' + mm + '-' + dd;
console.log(today);
document.getElementById('dateto1').value = today;
<div class="col-lg-4">
<label>Date</label>
<input type="date" name="dateto1" id="dateto1" class="form-control"></div>
The problem may be with the date format Use yyyy-mm-dd format.
Also there is no getDates function in the code
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
// today = yyyy + '/' + mm + '/' + dd;
today = `${yyyy}-${mm}-${dd}`;
document.getElementById('dateto1').value = today;
<div class="col-lg-4">
<label>Date</label>
<input type="date" name="dateto1" id="dateto1" class="form-control">
</div>
Related
Actually I made a function in which picks up the current date. Now i want to pickup the date after 6 months of my current date
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy ; //Here I am getting current date now i want to get the date exact after 6 months in the same format like this
var CurrentDate = new Date();
var month=CurrentDate.setMonth(CurrentDate.getMonth() + 6);//here I am getting date after 6 months but it is in different format like complete date, I want date after after 6 months like above format
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
var date=today;
var x = 6; //or whatever offset
var CurrentDate = new Date();
//alert(CurrentDate);
CurrentDate.setMonth(CurrentDate.getMonth() + x);
var d = String(CurrentDate.getDate()).padStart(2, '0');
var m = String(CurrentDate.getMonth() + 1).padStart(2, '0');
var yy = CurrentDate.getFullYear();
CurrentDate = m + '/' + d + '/' + yy;
var months=CurrentDate;
Just pass new generated timestamp in new Date() and it will give you new date
Like var today_after_month = new Date(timestamp);
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
var today = mm + '/' + dd + '/' + yyyy ; //Here i m getting current date now i want to get the date exact after 6 months in the same format like this
console.log('today', today);
var CurrentDate = new Date();
var month=CurrentDate.setMonth(CurrentDate.getMonth() + 6);//here i m getting date after 6 months but it is different format like complete date i want date after after 6 months like above format
var today_after_month = new Date(month);
var dd = String(today_after_month.getDate()).padStart(2, '0');
var mm = String(today_after_month.getMonth() + 1).padStart(2, '0');
var yyyy = today_after_month.getFullYear();
var today = mm + '/' + dd + '/' + yyyy ;
console.log('month', today);
I am doing a check-in/check-out datepicker. Check-in date picker is working but the problem is that the check-out one is allowing me to select check-in (and of course is not possible to check-out in the same day of check-in). So i want to add a day to check-out but i dont know how...
Can someone help me
This is the code i have:
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
today = yyyy + '-' + mm + '-' + dd;
document.getElementById("datainicio").setAttribute("min", today);
document.getElementById("datainicio").onchange = function () {
var input = document.getElementById("datafim");
input.min = document.getElementById("datainicio").value;
}
<div class="col-md-4">
<h4>Data Inicio</h4>
<p><input type="date" name="datainicio" id="datainicio"></p>
</div>
<div class="col-md-4">
<h4>Data Fim</h4>
<p><input type="date" id="datafim" name="datafim"></p>
</div>
Translations:
datainicio=check-in
datafim=check-out
Would momentjs be helpful to you? It is a small load to carry, but VERY helpful with dates in js.
Your code would then be something like this...
checkin = moment( pickeddate ).format( "YYYY-MM-DD" );
checkout = moment( pickeddate ).add('days', 1).format( "YYYY-MM-DD" );
Just a thought.
I want to create an incremental date. I have two buttons, one increases the date by 1 and another decreases it by 1. It works well until the end of the month or year; how to make it so it rolls over and can change depending on the month, as some months don't have the same amount of days?
Here's the code I have thus far
var today = new Date();
var dd = today.getDate()+days;
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd = '0'+dd
}
if(mm<10) {
mm = '0'+mm
}
today = dd + '/' + mm + '/' + yyyy;
$("#label").text(today);
days is a variable I'm using to add the days to the function
Just add the days directly to the Date, it will handle it:
var today = new Date();
today.setDate(today.getDate() + days);
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
You can also use this example:
<html>
<head>
<title>Sample</title>
</head>
<body>
<div id="label">
</div>
<button type="button" onclick="add()" name="button">Add</button>
<button type="button" onclick="sub()" name="button">Subtract</button>
</body>
<script type="text/javascript">
var days = 0;
var core = function(){
var today = new Date();
today.setDate(today.getDate() + days);
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) dd = '0'+dd
if(mm<10) mm = '0'+mm
$("#label").text(dd + '/' + mm + '/' + yyyy);
};
function add(){
days = days + 1;
core();
}
function sub(){
days = days - 1;
core();
}
add();
</script>
</html>
I have form and I want to set today's date in one field.
<input id="starttdate" type="date" name="startdate">
and I want to set today's date in this field, means that when this form open it show today's date in this filed by default.
My javascript code is this
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
var today = mm + '/' + dd + '/' + yyyy;
document.getElementById('starttdate').value = today;
<input id="starttdate" type="date" name="startdate">
in php just give value=date("Y-m-j")
<input id="starttdate" type="date" name="startdate" value="<?php echo date("Y-m-j"); ?>">
How can I use alert if the date of system is changed in javascript. The date can be change manually of automatically.
[Thought is, Mainly client want to get an notification after day of end and start of the day. Total sales of today will be notified to the client when tomorrow is starting. like if the date of 30 and when it will be 31 and mail will be send to the client that you have sold sum quantity yesterday or date of 30]
Here I have tried this code. I am new in javascript so suggestion is needed.
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
today = mm + '/' + dd + '/' + yyyy;
$("#today").change(function () {
alert(today);
});
You cannot listen to the event like you a trying, the only way you can do this is with a synthetic event that is fired when the date changes:
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
var changedate = function(date, to){
date = to;
document.trigger('dateChange');
}
document.on('dateChange', function () {
alert(today);
});