I am developing application for hotel booking..
I have used 2 datepickers for selecting check-in & check-out date..
But now i want my first date-picker's current value to be shown on that textbox by default & another textbox of second datepicker to be filled by date of 2 days after...
Just like these datepickers....
http://www.yatra.com/hotels.html
can anyone suggest me,how to do this??
I have used same datepickers as shown in link..
thanks in advance.
You can set a date like this (if the datepicker is initialized)
$('#your-selector')
.datepicker('setDate',new Date());
of a uninitialized datepicker use:
$('#your-selector')
.datepicker().datepicker('setDate',new Date());
to create a date 2 days before... make this:
var today = new Date();
var twoDaysBefore = new Date();
twoDaysBefore.setDate(today.getDate()-2);
and set the date
$('#your-selector')
.datepicker('setDate', twoDaysBefore);
you can create a function if you need this algorithm more times:
var getDate = function(daysDelta) {
var d = new Date();
(d.setDate(d.getDate()+daysDelta));
return d;
}
console.log(getDate(-3))
without jQuery (thanks), format your date object like this:
var d = getDate(-2);
var yyyy = d.getFullYear().toString();
var mm = (d.getMonth()+1).toString();
var dd = d.getDate().toString();
var dateAsString = yyyy + "-" + (mm[1]?mm:"0"+mm[0]) + "-" + (dd[1]?dd:"0"+1);
document.getElementById('id').setAttribute('value', dateAsString);
Todays Date
$("#date_pick").datepicker('setDate', new Date());
Date of 2 days after
var nextDate = new Date(new Date().getTime() + 48 * 60 * 60 * 1000);
$("#date_pick2").datepicker('setDate', nextDate);
try this code
$('first').datepicker({
onSelect: function(){
var start = $('first').val();
var date = new Date(start);
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
var edate= new Date(y, m, d+2);
$('second').val(edate);
}
});
If datepicker1 is your datepickers id and datepicker2 is the second one then
$("#datepicker1").val(new date());
var date2 = $('datepicker1').datepicker('getDate', '+2d');
date2.setDate(date2.getDate()+1);
$("#datepicker2").val(date2);
Related
I have one problem, which I can not resolve and have no idea where is mistake
One I create a tabel called posts and in codeigniter create a form Posts, after createing all post I want to display posts in index.php and I have table called
Date of departure, and date of return. One i create post date looking good but when I display in my index.php it's look like 0000-00-00 00:00:00
In begginig data type was Date, now i change to be DateTime but nothing happend, same problem
Maybe I make mistake in my JS script
Any comment ?
My script
<script>
$( function() {
$( "#datepicker" ).datepicker();
$( "#datepicker1" ).datepicker();
} );
</script>
When your field in MySQL is Date-time it aspects proper format before saving in database.
So convert string to date time format in php using.
date("Y-m-d H:i:s", strtotime($string));
Or you can do the same in Datepicker
$(function() {
$('#datepicker').datepicker({
dateFormat: 'yy-dd-mm',
onSelect: function(datetext){
var d = new Date(); // for now
var h = d.getHours();
h = (h < 10) ? ("0" + h) : h ;
var m = d.getMinutes();
m = (m < 10) ? ("0" + m) : m ;
var s = d.getSeconds();
s = (s < 10) ? ("0" + s) : s ;
datetext = datetext + " " + h + ":" + m + ":" + s;
$('#datepicker').val(datetext);
},
});
});
Fiddle for datepicker
Note if you want date only in MySQL field the omit hour min sec part of conversion
I need to find number of days between 2 selected dates. I have tried the following code but it returns error results for some dates.
function getDateObject(str) {
var arr = str.split("-");
return new Date(arr[0], arr[1], arr[2]);
}
$('#from_date,#to_date').bind("change paste keyup", function() {
var date1 = getDateObject($('#from_date').val());
var date2 = getDateObject($('#to_date').val());
var days = (date2 - date1) / 86400000;
alert(days); //it returns -1 for from:2016-01-31 - to:2016-02-01.
//other dates like from:2016-01-13 - to:2016-01-14 returns 1 correctly
}
please help me to resolve this issue.
You need to parse your dates correctly, currently:
new Date("2016", "01", "31");
will give
Wed Mar 02 2016 00:00:00 GMT+0530 (IST)
Just this will work fine
// str should be in yy-mm-dd
function getDateObject(str) {
return new Date(str);
}
finally I found a solution.
function getDateObject(str) {
var arr = str.split("-");
return new Date(arr[0], arr[1]-1, arr[2]);//just added -1 in arr[1] that is month.
}
$('#from_date,#to_date').bind("change paste keyup", function() {
var date1 = getDateObject($('#from_date').val());
var date2 = getDateObject($('#to_date').val());
var days = (date2 - date1) / 86400000;
alert(days);
}
this gave me the output I needed.
Thanks you all tried to help me.
Hello I need to spend a given XML through javascript to subtract the current date and time date.
What makes this code is to show the remaining time of a song streaming in minutes and seconds
And then with javascript parsing date and I transform in milliseconds and the current date will also rest in ms.
In Chrome date shows me perfectly, but in Mozilla and IE NaN console shows me and gives error.
I do not understand is that if I have parsed the date because it only works in Chrome. There must be a mistake.
PHP (I draw the start date of a song)
<?php
$xml = # simplexml_load_file('http://www.example.com');
foreach ($xml as $track){
$startTime = $track->starttime;
$songDuration = $track->playduration;
}
?>
JAVASCRIPT:
var spend javascript
var tiempoComienzo= "<?php echo $startTime ?>";
var cancionDuracion="<?php echo $songDuration ?>";
//parse delivered date from php
var d = new Date(Date.parse(tiempoComienzo));
//PHP get the date in milliseconds
var resultPHPms = d.getTime();
//get the current date
var f = new Date();
//step the current date to milliseconds
var resultJSms = f.getTime();
//adding the date of the song to the length of the song
var inicioMasCancion=parseInt(cancionDuracion) + parseInt(resultPHPms);
//It is the challenge to the current date
var TiempoRestante=inicioMasCancion-parseInt(resultJSms);
//pass the result to seconds
seconds=(TiempoRestante/1000)
var container = document.getElementById('time');
var seconds = parseInt(seconds+7);
//step seconds to minutes and seconds
var minutes = parseInt( seconds / 60 ) % 60;
var seconds = seconds % 60;
var contadorDeTiempo=(minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds);
countdown(seconds);
function countdown:
<script>
var timer;
function countdown(seconds) {
seconds--;
if(seconds >= -1) {
container.innerHTML =contadorDeTiempo;
}
}
timer = setInterval(countdown, 1000);
</script>
HTML:
<span id='time'></span>
In chrome it is perfectly displayed and works perfect. But in Mozilla and IE console NaN it is shown, specifically in this line:
var d = new Date(Date.parse(tiempoComienzo));
How I could solve? The resulting XML date is as follows:
2015-12-20 12:45:33.17
thanks very much for your help
I am using jquery ui plugin for date picker.
Now I need to read the year and month from user to create an array of all dates in that month in the format "yyyy-mm-dd". How can I get dates like this from datepicker?
Try this, hope this is what you need
var indate = $("#checkedin").val();
var date1 = indate.split("-");
var month = date1[1];
var year = date1[0];
var month_days = [0,31,28,31,30,31,30,31,31,30,31,30,31]; //days in each month, complete the array
var result = [];
for (var i = 1; i < month_days[month]; i++) {
var date = i
if (i < 10) {
date = "0"+i;
}
if (month < 10) {
month = "0"+month;
}
result.push(year+"-"+month+"-"+date);
}
For example, I select March 2013 month and I want to print the all dates of Sunday in this month. How can I print a specific day's date in month using jquery or php?
jQuery(function () {
jQuery("#datepicker").datepicker({
dateFormat: 'dd-mm-yy'
});
var day = new Date();
var month = day.getMonth() + 1;
var date = day.getDate() + '-' + month + '-' + day.getFullYear();
jQuery("#datepicker").val(da`enter code here`te);
});
I don't fully know what you mean, does this do what you want?
http://jqueryui.com/datepicker/
try this:
var date= new Date();
var month = date.getMonth() + 1;
var tmp;
var day = 1000 * 3600 * 24;
while(true) {
date = new Date(date.getTime() + day);
tmp = date.getMonth() + 1;
if(tmp!= month) {
break;
}
if(date.getDay() == 0)
document.write(date+"</br>");
}
see it work here
To do this I would find the first weekday of the month, and then find the first Sunday from that, then increment by 7 to find the rest of the Sundays.
An example in PHP:
$start = \DateTime::createFromFormat('d. m. Y', '01. 03. 2013');
$end = clone $start;
$end->add(new \DateInterval('P1M'));
// output all sundays between $start and $end
$periodInterval = \DateInterval::createFromDateString('first sunday');
$periodIterator = new \DatePeriod($start, $periodInterval, $end, \DatePeriod::EXCLUDE_START_DATE);
foreach ($periodIterator as $date) {
// output each date in the period
echo $date->format('d/m/Y') . ' ';
}
As far as you are using datepicker you may try to check every last box in the table, shown as calendar. It is gonna be the way when u will be sure that you have same data in javascript/php and on the screen