I have created one form as following:
<form action="test.php" method="POST" name="edituser">
<input type="text" id="to_date_picker" placeholder="Select Date" name="todate" class="input" size="50" />
<input name="addnew" type="submit" value="Add & Save" id="submitbtn" />
</form>
<script type="text/javascript">
$(function() {
$('#to_date_picker').datepick();
});
</script>
test.php: is as following:
$todate = $_POST['todate'];
$newtodate = date("Y-m-d", strtotime($todate));
echo $newtodate."<br>\n";
here it is giving wrong output. here date picker is a calender from which date is picked.
What is your dateformat? Because the default dateformat for datepick is "mm/dd/yyyy". strtotime accept this format for date:
American month, day and year mm "/" dd "/" y "12/22/78", "1/17/2006", "1/17/6"
I think that your datepick returns 01/17/2006.
Look for this.
You can search for mktime function, then you can parse your datetime for the proper format.
use this instead
$('#to_date_picker').datepick(
dateFormat: 'dd-mm-yy'
);
You can use new DateTime($todate); if you have PHP 5 >= 5.2.0 which is easier to use in my own opinion. Or if you don't want tu change the format return by your JavaScript, use the method DateTime::createFromFormat('j-M-Y', '15-Feb-2009'); which returns a DateTime object from a "custom" date (see documentation, link below).
Link : http://www.php.net/manual/fr/datetime.construct.php
http://www.php.net/manual/fr/datetime.createfromformat.php
Related
I have a column inside of my datebase that stores a date that user pick when they offer a job. It's currently formed in this way: "Fri Feb 15 2019".
I want to be able to compare it to the current date which is formed as: "2019-01-27"
I'm guessing that the best way to do this would be to transform the datepicker format to the date one, and then compare them. This is the code that I have for the date picker currently when someone registeres.
<div class="form-group">
<label for="location" class="control-label">DATE</label>
<input type="text" id="datepicker" class="form-control" placeholder="Pick a
date" name="datepicker">
</div>
<script>
var picker = new Pikaday({ field: document.getElementById('datepicker') });
</script>
Open the example in full page :
Use new Date and then you can extract the day the month and the year
and to compare with another date, you can just new Date("2006-05-1").getMonth() === new Date("2006-05-2").getMonth() && ...
var picker = new Pikaday({
field: document.getElementById('datepicker')
});
const dateInput = document.getElementById('datepicker');
function handleChange() {
const date = new Date(dateInput.value),
formattedDate = `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`;
console.log(formattedDate)
}
dateInput.addEventListener("change", handleChange);
<script src="https://cdn.jsdelivr.net/npm/pikaday/pikaday.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/pikaday/css/pikaday.css">
<input type="text" id="datepicker">
I would suggest using the javascript plugin called Momentjs. It allows for easy formatting of dates, without the base javascript hassle.
Add this to your HTML, near the bottom of your page, but before your script tags:
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
Then to format your datepicker value, inside your script, use:
var myDate = moment(picker.value).format('YYYY-MM-DD');
Here is the Momentjs documentation:
Momentjs Docs
I am using pikaday module for datepicker, but the format comes as inappropriate format. I tried adding this line of code but still not working:
.config(['pikadayConfigProvider', function (pikaday) {
pikaday.setConfig({
numberOfMonths: 1,
format: "YYYY/MM/DD"
});
}])
This is how my html looks like:
<div class="modal-body">
<form role="form">
<div class="form-group">
<input type="text" class="form-control" pikaday="myPickerObject" name="time" ng-model="clas.class_.time" placeholder="Enter time" tabindex="3">
</div>
</form>
</div>
Also tried to add as an inline attribute
format = "yyyy/mm/dd"
still not working.
Any help
You can use moment.js and set the format by setting
defaultDate : moment().format("MMM YYYY")
this will be the initial input date display format.
For displaying/processing the date in other desired formats, use
var field = document.getElementById('datepicker');
var picker = new Pikaday({
onSelect: function(date) {
field.value = this.getMoment().format('Do MMMM YYYY');
}
});
use moment.js to set the date format:
var picker = new Pikaday(
{
field: document.getElementById('eDate'),
toString(date, format) { // using moment
return moment(date).format('MM/DD/YYYY');
},
});
quick update, if you don't want to use moment.js, you can fdo the following
new Pikaday({
field: document.getElementById('eDate'),
toString: function(date) {
var parts = [('0'+date.getDate()).slice(-2), ('0'+(date.getMonth()+1)).slice(-2), date.getFullYear()];
return parts.join("-");
}
})
this will produce 18-07-1980. You can change from '-' to '/' by changing return parts.join("-"); and you can rearrange parts to apply mm/dd/yyyy via parts array
In my form view I use bootstrap daytime picker
<input type="text" value="" placeholder="select date you want to send" class="form-control" name="send_date" id="datetimepicker" />
<script>
$.datetimepicker.setLocale('<%= session[:locale] %>');
$('#datetimepicker').datetimepicker({
minDate: 0,
dayOfWeekStart : 1,
lang:'<%= session[:locale] %>',
startDate: '-0m',
format:'d-m-Y H:i:s',
});
</script>
In my SMS controller:
def send_sms
message = params[:message]
number = params[:number]
send_date = params[:send_date]
format = "%Y-%m-%d %H:%M:%S UTC"
send_date = DateTime.strptime(send_date, format)
SendSmsTimeJob.set(wait_until: send_date).perform_later(number, message)
flash[:success] = "mesaj gitti"
redirect_to current_user
end
When I submit the form it says:
ArgumentError in SmsController#send_sms
invalid date
I think that problem is I cannot convert input value to ruby time value? How can I overcome with this?
Change your format to this.
format = "%d-%m-%Y %H:%M:%S"
In your datepicker you format your date as format:'d-m-Y H:i:s' which means days-month-years
In your ruby code you parse it differently:
format = "%Y-%m-%d %H:%M:%S UTC"
First you have the date order reversed: year-month-days
Second you add another expected value 'UTC' which is not included in the string you are trying to parse.
Change your format to format = "%d-%m-%Y %H:%M:%S" as suggested by Rubysmith
You can try Date.parse params[:send_date] instead.
I am using Eonasdan's Bootstrap Date/time picker:
https://github.com/Eonasdan/bootstrap-datetimepicker
I would like to be able to read the selected picked date in a format of my choosing which in this case is DD/MM/YYYY. However it seems to output with the day of week, month name, day, year, time and GMT offset.
I have created an example of the script here:
http://jsfiddle.net/0Ltv25o8/1649/
<div class="row">
<div class="col-md-12">
<p><input type="text" class="datefield" placeholder="Date 1" id="date1"/></p>
<p><input type="text" class="datefield" placeholder="Date 2" id="date2"/></p>
</div>
</div>
$('.datefield').datetimepicker({
format: 'DD/MM/YYYY'
})
$(document).on('dp.change', '.datefield', function(e) {
var id=$(this).attr('id');
var theDate=e.date;
alert(id + " / " + String(theDate));
});
When you select a date I output the ID of the date text field and the date string. I am able to set the date format for how the date appears in the date text field but not when I read it.
$(document).on('dp.change', '.datefield', function(e) {
var id=$(this).attr('id');
var theDate=e.date;
alert(id + " / " + e.date.format('DD/MM/YYYY'));
});
You can format this date anywhere
I want to create a very simple javascript form validation. I have two input boxes that are populated by a datetime picker. the datetime picker populates the input box with in the format 2011-12-21 09:58. I have an input box for collection date and arrival date.
I want to validate that the 'deliverdatetime' entered is greater than the 'collectdatetime'.
The output of a failed validation should just be in a popup window saying 'deliverdate' before 'coolectdate'.
Can this be done with javascript and if so can someone give me a brief example?
my basic html form code is:
...
<td>
<input type="text" id="collectdatetime" name="collectdatetime">
</td>
<td>
<input type="text" id="deliverdatetime" name="deliverdatetime" >
</td>
...
Thanks in advance for the assistance and happy holidays to everyone.
Regards,
Ryan Smith
GIven the ISO8601 format of the date/time string, you can just compare the values as strings. It's one of the beauties of using an ISO8601 format. You have asked for the delivery time to be after the collection time, which seems backwards to me but anyhow…
<form onsubmit="return checkDates(this)">
<table>
<tr>
<td>
<input type="text" id="collectdatetime" name="collectdatetime">
<td>
<input type="text" id="deliverdatetime" name="deliverdatetime">
<td>
<input type="submit">
</table>
</form>
<script>
function checkDates(form) {
if (form.collectdatetime.value >= form.deliverdatetime.value) {
alert('Collection time must be after deliver time');
return false;
}
}
</script>
You could do something like this:
var collectTime = (new Date(document.getElementById("collectdatetime").value)).getTime(),
deliverTime = (new Date(document.getElementById("deliverdatetime").value)).getTime();
if(deliverTime > collectTime) {
//Ok!
}
This creates 2 Date objects, passing the selected values to the constructor, and then compares the times. The getTime method returns a number representing the time that has passed since 1 January 1970 00:00:00 in milliseconds.
Here's a working example.
You can create Date object from this values and compare them:
var collect = new Date($('#collectdatetime').val());
var delivery = new Date($('#deliverdatetime').val());
if (delivery < collect) {
alert('Delivery date before collect!');
}