bootstrap linked datetime picker not working properly - javascript

hi i am trying to develop bootstrap jquery for linked datetime pickers but is not working correctly am using jsfiddle http://jsfiddle.net/srknthcse/CujDK/311/ i have selected No-library ...any help will be appreciated .thank you
<div class="container">
<div class='col-md-5'>
<div class="form-group">
<div id="startdatetime-from" class="input-append date">
<input type="text" id="datetimepicker-input" readonly ></input>
<span class="add-on">
<i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
</span>
<div class='col-md-5'>
<div class="form-group">
<div id="startdatetime-from1" class="input-append date1">
<input type="text" id="datetimepicker-input1" readonly ></input>
<span class="add-on">
<i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
</span>
</div>
</div>
var d = new Date();
var month = d.getMonth();
var day = d.getDate();
var year = d.getFullYear();
var hours= d.getHours();
var minutes=d.getMinutes();
$(function() {
$('#startdatetime-from').datetimepicker({
language: 'en',
showMeridian:'true',
autoclose:'true',
format: 'hh:mm PP dd-MM-yyyy '
});
$("#startdatetime-from").data('datetimepicker').setLocalDate(new Date(year, month, day, hours, minutes));
var d1=new Date();
var d2=new Date(d1);
var addedhour=d2.setHours(d1.getHours()+4);
$('#startdatetime-from1').datetimepicker({
language: 'en',
showMeridian:'true',
autoclose:'true',
format: 'hh:mm dd-MM-yyyy '
});
$("#startdatetime-from1").data('datetimepicker').setLocalDate(new Date(year, month, day, hours, minutes));
$("#startdatetime-from").on("dp.change", function (e) {
$('#startdatetime-from1').data("DateTimePicker").minDate(e.date);
});
$("#startdatetime-from1").on("dp.change", function (e) {
$('#startdatetime-from').data("DateTimePicker").maxDate(e.date);
});
});

<div class="container">
<div class='col-md-10'>
<div class="form-group">
<div id="startdatetime-from" class="input-append date">
<input type="text" id="datetimepicker-input" readonly></input>
</div>
<div></div>
<div class='col-md-10'>
<div class="form-group">
<div id="startdatetime-from1" class="input-append date1">
<input type="text" id="datetimepicker-input1" readonly></input>
<span class="add-on">
<i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
</span>
</div>
</div>
</div>
var d = new Date();
var month = d.getMonth();
var day = d.getDate();
var year = d.getFullYear();
var hours = d.getHours();
var minutes = d.getMinutes();
$(function () {
$('#startdatetime-from').datetimepicker({
language: 'en',
autoclose: 'true',
format: 'HH:mm PP dd-MM-yyyy '
});
$("#startdatetime-from").data('datetimepicker').setLocalDate(new Date(year, month, day, hours, minutes));
$('#startdatetime-from1').datetimepicker({
language: 'en',
autoclose: 'true',
format: 'HH:mm PP dd-MM-yyyy '
});
$("#startdatetime-from1").data('datetimepicker').setLocalDate(new Date(year, month, day, hours+4, minutes));
});

Related

compare date entered in textbox with javascript

asp.net Code:
<div class="form-group">
<label class="control-label col-md-5">Received Date</label>
<div class="col-md-7">
<div class="input-group date date-picker" id="recvdate" data-date-format="dd-mm-yyyy" runat="server">
<asp:TextBox ID="txtRecvdDate" CssClass="form-control tooltips" data-original-title="dd-mm-yyyy" data-placement="top" runat="server"></asp:TextBox>
<span class="input-group-btn">
<button class="btn default dtp-btn " id="Button2" type="button" runat="server"><i class="icon-calendar"></i></button>
</span>
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-5">Expiry Date</label>
<div class="col-md-7">
<div class="input-group date date-picker" id="expdate" data-date-format="dd-mm-yyyy" runat="server">
<asp:TextBox ID="txtExpiryDate" CssClass="form-control tooltips" onkeyup="validation();" data-original-title="dd-mm-yyyy" data-placement="top" runat="server"></asp:TextBox>
<span class="input-group-btn" id="expiryDatePicker">
<button class="btn default dtp-btn " id="Button1" type="button" runat="server"><i class="icon-calendar"></i></button>
</span>
<asp:Label ID="lblreasonMessagedvTabNavigationAction" Font-Bold="true" runat="server"></asp:Label>
</div>
</div>
</div>
Javascript
function validation() {
var startDate = $("[id*=txtRecvdDate]").val();
var endDate = $("[id*=txtExpiryDate]").val();
var newDate = new Date(startDate);
var newToDate = new Date(endDate);
if (newDate != '' && newToDate !='') {
if (newDate < newToDate)
{
alert("check");
}
}
return false;
}
I am unable the compare the expiry date with received date textbox. In which expiry date should be greater than received date
You have date as dd-mm-yyyy format from textbox(startdate). so when you use it in conversion like new Date(dd-mm-yyyy). It will throw invaliddate. So before converting change its format to yyyy-mm-dd by using below code. because new Date accept datestring in yyyy-mm-dd format.
function validation() {
var startDate = $("[id*=txtRecvdDate]").val();
var newdatestring = startDate.split("-").reverse().join("-");
var newDate= new Date(newdatestring);
var endDate = $("[id*=txtExpiryDate]").val();
var newToDatestring = endDate.split("-").reverse().join("-");
var newToDate= new Date(newToDatestring);
if (newDate != '' && newToDate !='') {
if (newDate < newToDate)
{
alert("check");
}
}
return false;
}
Note: If you are using / instead of - then replace - with / in the above code

Comparing two custom format dates jquery

I have tried to validate my dates if start date is less than end date it should alert but it only work with in same month, but when i compare dates of one month date to another month date it keeps stops me select less date even it is already.
here is what i have done so far,
$('#datepicker2').change(function () {
var startDate = new Date($('#datepicker').val());
var endDate = new Date($('#datepicker2').val());
if (startDate < endDate){
alert("selected date is above pickup date, ");
}
});
here is my html
<div class="col-md-4">
<div class="row">
<div class="col-md-6">
<input type="text" id="datepicker" required name="pickupDate" placeholder="Pickup Date">
</div>
<div class="col-md-6">
<input class="" id="datetimepicker4" autocomplete="off" required name="pickupTime" placeholder="Pickup Time">
</div>
</div>
</div>
<div class="col-md-4">
<div class="row ct-date-row">
<div class="col-md-6">
<input type="text" id="datepicker2" required name="dropDate" placeholder="Drop off Date">
</div>
<div class="col-md-6">
<input class="" autocomplete="off" id="datetimepicker5" required name="dropTime" placeholder="Drop Time">
</div>
</div>
</div>
my date format is d/m/y if this is making issue then how could i possibly define formate within new date and compare them. Thanks
I want when same date it hides previous time on time drop down of jquery.
$('#datetimepicker5').timepicker({
timeFormat: 'HH:mm',
});
$('#datetimepicker4').timepicker({
timeFormat: 'HH:mm',
});
format of my time i am not being to get time which is selected,
jQuery('document').ready(function(){
jQuery('#datetimepicker5').on('change paste keyup', function() {
var x = jQuery("#datetimepicker5").val();
alert(x);
});
});
So, you are using jQuery Ui DatePicker, there is native way to get date with getDate, here you go:
For compare your dates if equal or same you need to use Date.parse() , you need to do this:
if (Date.parse(startDate) === Date.parse(endDate))
$(function() {
$("#datepicker").datepicker({
dateFormat: "d/m/y"
});
$("#datepicker2").datepicker({
dateFormat: "d/m/y"
});
$('#datetimepicker5').timepicker({
timeFormat: 'HH:mm',
});
$('#datetimepicker4').timepicker({
timeFormat: 'HH:mm',
});
});
$('#datepicker2').on('paste keyup change', function() {
var startDate = $('#datepicker').datepicker("getDate");
var endDate = $('#datepicker2').datepicker("getDate");
if (startDate > endDate) {
alert("selected date is above pickup date, ");
} else if (Date.parse(startDate) === Date.parse(endDate)) {
alert("same");
var dt = new Date();
var phours = dt.getHours() + 3;
var time = phours + ":" + "00";
alert(time);
jQuery('#datetimepicker5').timepicker({
minTime: time
});
jQuery('#datetimepicker4').timepicker({
minTime: time
});
$('#datetimepicker4').val(time)
$('#datetimepicker5').val(time)
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://www.jonthornton.com/jquery-timepicker/jquery.timepicker.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js" integrity="sha256-KM512VNnjElC30ehFwehXjx1YCHPiQkOPmqnrWtpccM=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" integrity="sha256-rByPlHULObEjJ6XQxW/flG2r+22R5dKiAoef+aXWfik=" crossorigin="anonymous" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-timepicker/1.10.0/jquery.timepicker.min.css" integrity="sha256-zV9aQFg2u+n7xs0FTQEhY0zGHSFlwgIu7pivQiwJ38E=" crossorigin="anonymous" />
<div class="col-md-4">
<div class="row">
<div class="col-md-6">
<input type="text" id="datepicker" required name="pickupDate" placeholder="Pickup Date">
</div>
<div class="col-md-6">
<input class="" id="datetimepicker4" autocomplete="off" required name="pickupTime" placeholder="Pickup Time">
</div>
</div>
</div>
<div class="col-md-4">
<div class="row ct-date-row">
<div class="col-md-6">
<input type="text" id="datepicker2" required name="dropDate" placeholder="Drop off Date">
</div>
<div class="col-md-6">
<input class="" autocomplete="off" id="datetimepicker5" required name="dropTime" placeholder="Drop Time">
</div>
</div>
</div>
You can convert your both dates to seconds and then compare it. then it will work like charm:
i.e.
var date1 = new Date("2015-08-25T15:35:58.000Z");
var seconds1 = date1.getTime() / 1000; //1440516958
Use Date.parse()
The Date.parse() method parses a string representation of a date and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC
$('#datepicker2').change(function () {
var startDate = new Date($('#datepicker').val());
var endDate = new Date($('#datepicker2').val());
if (Date.parse(startDate) < Date.parse(endDate)){
alert("selected date is above pickup date, ");
}
});

Want to calculate date of birth from a given age

I'm trying to calculate the date of birth from a given age.
My logic is
When we enter an age say '5'. The date of birth will be calculated as exactly 5 years from the current date. Example current date is 20/8/2018.
Then date of birth will be 20/8/2013 if the age is 5.
So below is my code:
function validatedob() {
var dob_value = document.getElementById('dob').value;
var age_value = document.getElementById('age').value;
var current_date = new Date();
var birth_date = new Date();
var birth_year = current_date.getFullYear() - age_value;
var birth_month = current_date.getMonth();
var birth_day = current_date.getDate();
birth_date.setDate(current_date.getFullYear() - age_value);
birth_date.setMonth(current_date.getMonth());
birth_date.setFullYear(current_date.getDate());
document.getElementById('dob').setDate(birth_day);
document.getElementById('dob').setMonth(birth_month);
document.getElementById('dob').setFullYear(birth_year);
}
<div class="form-inline" id="Age">
<label for="age">Age</label>
<input id="age" type="integer" name="age" onchange="validatedob()">
</div>
<div class="form-inline" id="birth">
<label for="DOB">Date of Birth</label>
<input id="dob" type="date" value="Unknown" name="date_of_birth" max="31-12-2030">
</div>
This is not working. I'm not goodwith html properties.
Can someone help me? how to achieve this?
Here is the simple answer:
> d1 = new Date(2018, 00, 01)
Date 2018-01-01T00:00:00.000Z
> d2 = new Date(d1.getFullYear() - 5, d1.getMonth(), d1.getDate())
Date 2013-01-01T00:00:00.000Z
But, I would suggest to use a library such as Luxon.
Date manipulation is an absolute nightmare if you do it by yourself. This video from Computerphile summarizes it better than I could:
https://www.youtube.com/watch?v=-5wpm-gesOY
Here is a really simple solution that works and some changes to your HTML.
Changes to HTML:
The div and label both had the id "age", this must be unique.
type="integer"doesn't exist, so i changed it to type="number".
And value="unknown"isn't the right format "yyy-MM-dd", so i just removed it.
for="DOB"to for="dob", now that works aswell
Code:
HTML
<div class="form-inline">
<label for="age">Age</label>
<input id="age" type="number" name="age"onchange="validatedob()">
</div>
<div class="form-inline" id="birth">
<label for="dob">Date of Birth</label>
<input id="dob" type="date" name="date_of_birth" max="31-12-2030">
</div>
JavaScript
function validatedob() {
var age_value = document.getElementById("age").value;
var current_date = new Date();
var birth_date = new Date(current_date.getFullYear() - age_value, current_date.getMonth(), current_date.getDate() + 1);
document.getElementById("dob").valueAsDate = birth_date;
}
Here is some sample code for your reference
https://codepen.io/ji_in_coding/pen/wEvoMK
<div class="form-inline" id="Age">
<label for="age">Age</label>
<input id="age" type="integer" name="age" onchange="validatedob()">
</div>
<div class="form-inline" id="birth" >
<label for="DOB">Date of Birth</label>
<input id="dob" type="input" name="date_of_birth">
</div>
Javascript
function validatedob()
{
var age_value = document.getElementById("age").value;
var today = new Date();
var calculated_birthday = new Date(
parseInt(today.getFullYear())+parseInt(age_value),
today.getMonth(),
today.getDate());
document.getElementById("dob").value = calculated_birthday.toLocaleDateString("en-US");
}
You're issue is that you are trying to setDate and cie on a HTMLElement. The input dob expect a string formatted as yyyy-MM-dd. You can try something like that:
var $dob = document.getElementById('dob');
var $age = document.getElementById('age');
function validatedob() {
var age = $age.value;
var date = new Date();
date.setFullYear(date.getFullYear() - age);
$dob.value = formatDate(date);
}
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}
<div class="form-inline" id="Age">
<label for="age">Age</label>
<input id="age" type="integer" name="age" onchange="validatedob()">
</div>
<div class="form-inline" id="birth">
<label for="DOB">Date of Birth</label>
<input id="dob" type="date" value="Unknown" name="date_of_birth" max="31-12-2030">
</div>
PS: For performance purpose avoid to repeat document.getElementById(...) in each method call. Do it once as done in my example.

Create dependent datepickers using bootstrap-datepicker

I need to create two date pickers using the datepicker by https://github.com/uxsolutions/bootstrap-datepicker so that date selected in the first datepicker will be used as start date of second. The dates before the date picker of first one disabled in the second.
The HTML for first date-picker:
<div class="col-sm-6 row-margin" id="div-last-day">
<div class="input-group date">
<input type="text" class="test-width form-control input-xs" name="last-day-worked" id="last-day" placeholder="Select your last Date" readonly><span class="input-group-addon input-xs"><i class="glyphicon glyphicon-calendar"></i></span>
Second date-picker:
<div class="col-sm-6 row-margin" id="div-separation-day">
<div class="input-group date">
<input type="text" class="test-width form-control input-xs" name="separation-day-worked" id="separation-day" placeholder="Select your Separation Date" readonly><span class="input-group-addon input-xs"><i class="glyphicon glyphicon-calendar"></i></span>
</div>
</div>
Here is some of my code:
$("#div-last-day .input-group.date").datepicker({
format: "dd-M-yyyy",
todayHighlight: true,
autoclose: true,
});
$('#div-last-day .input-group.date').datepicker()
.on('changeDate', function (e) {
var lastdate = $('#div-last-day .input-group.date').datepicker('getDate');
var date = new Date(lastdate);
var curr_date = date.getDate();
var curr_month = date.getMonth();
var curr_year = date.getFullYear();
perfect_date = curr_date + "-" + m_names[curr_month]
+ "-" + curr_year;
$("#div-separation-day .input-group.date").datepicker({
format: "dd-M-yyyy",
todayHighlight: true,
startDate: perfect_date,
autoclose: true,
});
});
Here is the fiddle I am working on https://jsfiddle.net/5tpn12L8/
The fiddle works for the first time but when I change the date in the first date-picker it doesn't update the second date-picker.
You can do it as below:
HTML:
<input class="form-control" id="txtFromDate" />
<input class="form-control" id="txtToDate" />
JQUERY:
$(document).ready(function () {
$("#txtFromDate").datepicker({
format: 'dd/mm/yyyy',
autoclose: 1,
//startDate: new Date(),
todayHighlight: false,
//endDate: new Date()
}).on('changeDate', function (selected) {
var minDate = new Date(selected.date.valueOf());
$('#txtToDate').datepicker('setStartDate', minDate);
$("#txtToDate").val($("#txtFromDate").val());
$(this).datepicker('hide');
});
$("#txtToDate").datepicker({
format: 'dd/mm/yyyy',
todayHighlight: true,
//endDate: new Date()
}).on('changeDate', function (selected) {
$(this).datepicker('hide');
});
});
jsfiddle DEMO
actually timepicker has it's own documentation you can use as
$('#timepicker_input').datetimepicker({
format: "dd-M-yyyy",
todayHighlight: true,
autoclose: true,
onSelect: function(dateText, inst){
//here perform the change you want
}
});

Generate all dates of a week using current date also show week starting date on Monday and weekends on Sunday

Generate all dates of a week using current date also show week starting date on Monday and weekends on Sunday using JavaScript or AngularJS.
User can enter any date (any year and month)
Week Should start on Monday and end on Sunday
It should work of all years & months
Ex: Selected date ( Friday 1st Jan 2016) (default it should select Current date) strong text
It should Show
28/12/2015(monday)
29/12/2015(tuesday)
30/12/2015(wednesday)
31/12/2015(thursday)
1/1/2016(friday)
2/1/2016(saturday)
3/1/2016(sunday)
$('#datepicker').datepicker({}).datepicker("setDate", new Date());
window.onload = lastDate;
function lastDate() {
lDate = document.getElementById('datepicker').value;
var myDate = new Date(lDate).toLocaleDateString('en-US');
var xDate = moment(lDate).format("D-MMM-YYYY");
var curr = new Date(myDate);
var sun = curr.getDate() - curr.getDay();
var sat = sun + 6;
var mon = sun + 1;
var tue = sun + 2;
var wed = sun + 3;
var thu = sun + 4;
var fri = sun + 5;
var sun1 = sun + 7;
var last = sun1;
sunday = new Date(curr.setDate(sun)).toLocaleDateString('en-US');
monday = new Date(curr.setDate(mon)).toLocaleDateString('en-US');
tuesday = new Date(curr.setDate(tue)).toLocaleDateString('en-US');
wednesday = new Date(curr.setDate(wed)).toLocaleDateString('en-US');
thursday = new Date(curr.setDate(thu)).toLocaleDateString('en-US');
friday = new Date(curr.setDate(fri)).toLocaleDateString('en-US');
saturday = new Date(curr.setDate(sat)).toLocaleDateString('en-US');
sunday1 = new Date(curr.setDate(sun1)).toLocaleDateString('en-US');
lastday1 = new Date(curr.setDate(sun1)).toLocaleDateString('en-US');
document.getElementById("day1").innerHTML = sunday;
document.getElementById("day2").innerHTML = monday;
document.getElementById("day3").innerHTML = tuesday;
document.getElementById("day4").innerHTML = wednesday;
document.getElementById("day5").innerHTML = thursday;
document.getElementById("day6").innerHTML = friday;
document.getElementById("day7").innerHTML = saturday;
document.getElementById("day1").innerHTML = sunday1;
}
<body>
<div class="container">
<h3>This week dates </h3>
<input id="datepicker" class="currentDate" onchange="lastDate()">/input>
</div>
<div Class="container-fluid well">
<div class="form-inline">
<div class="form-control">
<span class="label label-default">Monday</span>
<span id="day2"> </span>
</div>
<div class="form-control">
<span class="label label-default">Tuesday</span>
<span id="day3"> </span>
</div>
<div class="form-control">
<span class="label label-default">Wednesday</span>
<span id="day4"> </span>
</div>
<div class="form-control">
<span class="label label-default">Thursday</span>
<span id="day5"> </span>
</div>
<div class="form-control ">
<span class="label label-default">Friday</span>
<span id="day6"> </span>
</div>
<div class="form-control">
<span class="label label-default">Saturday</span>
<span id="day7"> </span>
</div>
<div class="form-control">
<span class="label label-default">Sunday</span>
<span id="day1"></span>
</div>
</div>
</div>
</body>
var curr = new Date;
var Sunday = new Date(curr.setDate(curr.getDate() - curr.getDay()));
var Monday = new Date(curr.setDate(curr.getDate() - curr.getDay()+1));
var Tuesday = new Date(curr.setDate(curr.getDate() - curr.getDay()+2));
var Wednesday = new Date(curr.setDate(curr.getDate() - curr.getDay()+3));
var Thursday = new Date(curr.setDate(curr.getDate() - curr.getDay()+4));
var Friday = new Date(curr.setDate(curr.getDate() - curr.getDay()+5));
var Saturday = new Date(curr.setDate(curr.getDate() - curr.getDay()+6));
console.log(Sunday);
console.log(Monday);
console.log(Tuesday);
console.log(Wednesday);
console.log(Thursday);
console.log(Friday);
console.log(Saturday);

Categories

Resources