how can I remove days from input type date? I added default data:
<input required class="data_inizio" type="date" name="inizio" value="2018-01-31">
But I would to remove previous days before that..
You could use the min attribute, to only allow future dates. Like so: <input required class="data_inizio" type="date" min="2018-01-31" name="inizio" value="2018-01-31">. You will only need to replace the min value with a dynamic value which represents the current date. You could use something like php for that: .
So it would become:
<input required id="date-input" class="data_inizio" type="date" min="<?=date('Y-m-d', time())?>" name="inizio" value="2018-01-31">`.
You can find the browser support over here: Browser_compatibility
Or setting the date using 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;
document.getElementById("date-input").setAttribute('min', today);
Related
The following code gets the date in the form 6/4/2016
<script>
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var currentDate = (month + "/" + day + "/" + year)
document.write(month + "/" + day + "/" + year)
document.write(currentDate)
</script>
I've tried several different ways to submit the date as a hidden field in a form to use as the primary key in a SQL database.
<input type="hidden" name="DateSubmitted" value=($month + "/" + $day + "/" + $year)>
^ shows $month as what was submitted in the array
<?php $currentDate = date('Y-m-d'); ?>
<input type="hidden" name="mDateJoined" value="<?php echo $currentDate; ?>">
^is blank in the array
<input type="hidden" name="DateJoined" value=currentDate>
^displays currentdate as the text submitted.
Rather than submit the date from the form to the php page, I just included
$currentDate = date('Y-m-d');
on the php page for sql insertion.
I have to assume you want to read the clock of the client computer, which might be wrong, not just have the date and time the form was submitted? If the latter is the case use the suggestion of assemblyhelpneededplease, it is more reliable.
The mistake you seem to make is to mix HTML, Javascript, and PHP. These three are quite different beasts. You should have a clear idea about what is which. This PHP script will get the date from the computer of your visitor:
<?php
echo '<script>
var currentTime = new Date();
var month = currentTime.getMonth()+1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var currentDate = month+"/"+day+"/"+year);
document.write("<input type=\"hidden\" name=\"mDateJoined\" value=\""+currentDate+"\">");
</script>';
So this is a PHP script outputting Javascript which generates HTML. Complicated. Better follow the suggestion of assemblyhelpneededplease!
aData[8] is the column number which shows date in dd-mm-yyyy format.
I want to change it to yyyy-mm-dd format for validation purpose.
var dueDate = new Date(aData[8]);
console.log(aData[8]);
var dt_to = $.datepicker.formatDate('dd-mm-yy', new Date(dueDate));
var dueDatetimestamp = dt_to.getTime();
I like to use moment.js to manipulate dates.
You could do something like that with it:
moment("13-08-2014", "DD-mm-YYYY").format("YYYY-mm-DD")
Consider this:
var str = '13-02-2014';//your format dd-mm-yyyy
var newar = str.split('-');
var tempDate = newar[2] + '-' + newar[1] + '-' + newar[0];
var d = new Date(tempDate);
console.log(d);//date object
console.log(tempDate);//new format yyyy-mm-dd
DEMO
you can use date.js to achieve :
var dateAr = '2014-01-06'.split('-');
var newDate = dateAr[0] + '-' + dateAr[1] + '-' + dateAr[2];
DEMO
Personnally, I'm using jQuery.format.date(value,'date-format'). But this is a jQuery's plugin available at https://github.com/phstc/jquery-dateFormat
Very simple to use (example from Github) :
<script>
document.write($.format.date("2009-12-18 10:54:50.546", "Test: dd/MM/yyyy"));
document.write($.format.date("Wed Jan 13 10:43:41 CET 2010", "dd~MM~yyyy"));
</script>
I have a simple textfield with the type defied to be as date which attached with a jquery datepicker(). Now i have set the dateFormat() to be dd-mm-yy. All this works fine. But as i know MySql requires date in yyyy-mm-dd. So i was just wondering if i can convert that date format either using php or javascript to enter into the database.
E.g var date = $('#order_date').val(); gives value 26-2-2012. I need to convert this into 2012-02-26.
Note: There is also a 0 being added to the month.
Use PHP:
$date = $_POST['date']; // e.g. 06-08-2011
$mysql_date = date('Y-m-d', strtotime($date));
or
$date = explode('-', $_POST['date']);
$mysql_date = $date[2].'-'.$date[1].'-'.$date[1];
use $mysql_date to insert into the database.
You can do in javascript by parsing the input and loading it into a Date() object, and then using the getMonth, getDate, and getFullYear functions, as follows:
//Assume input is dd-mm-yyyy
var dateParts = $('#order_date').val().split("-");
var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);
//format using getMonth(), getDate() and getFullYear() methods)
var formattedMonth = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1);
var formattedDate = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
var output = date.getFullYear() + '-' + formattedMonth + '-' + formattedDate;
You can see more at this excellent StackOverflow post
did u try date.format("yyyy-mm-dd"); ?
u can also use "isoDate" in place of "yyyy-mm-dd"
and if u are using mysql u can use DATE_FORMAT(date,'%Y-%m-%d')
Once the user has selected the date from the datepicker, i want to add 41 days.
<script language="javascript">
$(document).ready(function() {
$("#startdate").datepicker({ dateFormat: 'd/m/y'});
$('#startdate').datepicker({
onSelect: function(dateStr) {
var nights = parseInt($('#numofdays').val());
var depart = $.datepicker.parseDate('d/m/y', dateStr);
depart.setDate(depart.getDate('d/m/y') + nights);
$('#calc').val(depart);
}
});
});
</script>
Start: <input type="text" id="startdate" class="datepicker"><br />
<input type="hidden" id="numofdays" value="41"><br />
Calc: <input type="text" id="calc">
You might just need to combine the options in the same initiating function:
$("#startdate").datepicker({
dateFormat: 'd/m/y',
onSelect: function(dateStr, inst) {
var nights = parseInt($('#numofdays').val());
var depart = $.datepicker.parseDate('d/m/y', dateStr);
depart.setDate(depart.getDate('d/m/y') + nights);
$('#calc').val(depart.toLocaleDateString());
}
});
See this in action: http://jsfiddle.net/william/L9Szd/.
$('#startdate').datepicker('getDate') will return a Date object
d = $('#startdate').datepicker('getDate');
d.setDate(d.getDate()+nights); // add int nights to int date
alert(d);
Adding d.getDate() and nights will jump time forward. So if 9/16/2011 + 41 days you'll get 10/25/2011
would seem strange as a user to select a date, and then the box says a date that's 41 days later instead of the one I picked. why not do this addition server side with PHP?
$date_string = date('Y-m-d', strtotime('+41 days', strtotime($_POST['date_input'])));
This is the only simple way I found to do this:
To set date to 41 days ahead of the selected date:
1.Fetch selected date from a datepicker field
var newdate = new Date($("#datepicker_date_field_1").datepicker("getDate"));
2.Increase the date got from above
newdate.setDate(newdate.getDate() + 41);
3.Assign the newly formed date to another (or same) datepicker field
$("#datepicker_date_field_2").datepicker("setDate",newdate);
Is it possible through Jquery/Javascript that I can validate a date with the following conditions
example date : 10/10/2011
Conditions : Not in the past (accept
today onwards) Not more than 18 months
(this can be variable)
Because the ways I found out was
var e = new Date();
Alert(e.getMonth() + 2);
but this is not what I wanted
you might want to have a look at date.js
it's a great library for manipulating dates in javascript.
http://www.datejs.com/
here's the documentation:
http://code.google.com/p/datejs/wiki/APIDocumentation
it can also do the comparisons for you!
have a look at Date.compare and Date.addMonths
You can use JQuery's Datepicker, it has validation included and is pretty nice overall.
It can be done like this:
var toValidate = new Date('10/10/2011');
var months = 18;
var now = new Date();
var minDate = new Date(now.getFullYear(), now.getMonth(), now.getDate());
var maxDate = new Date(now.getFullYear(), now.getMonth() + months, now.getDate());
var valid = minDate < toValidate && toValidate < maxDate;