Convert ajax calendar value to datetime in textbox blur event using JavaScript - javascript

I have an ASP.net textbox with AJAX calendar extender control.
<asp:TextBox ID="tbxReceivedDate" CssClass="selectstyle" runat="server" MaxLength="100" Width="200" onblur="parseStringtoDateTime();"></asp:TextBox>
<cc1:CalendarExtender ID="CalendarExtender1" TargetControlID="tbxReceivedDate" Format="ddd MM/dd/yyyy hh:mm:ss tt" runat="server"></cc1:CalendarExtender>
I want to convert this string into a proper date format (ex., 08/17/2014 9:43:00 AM) using JavaScript in textbox blur event.
So far, I have below code but it is not giving me the desired result.
<script type="text/javascript">
function parseStringtoDateTime() {
var t = new Date($('#<%= tbxReceivedDate.ClientID %>').val());
alert(t);
}
</script>
What can I change to get the desired result?

You can get the date in javascript through the AJAX behavior, like this:
var date = $find("behaviorID").get_selectedDate();
For this to work, in your CalendarExtender you should define the BehaviorID attribute like this BehaviorID="behaviorID"

Related

jQuery data('datepicker').getDate() return Invalid date

I have this input that I am using for dates.
<input id="txtsdate" class="form-control" type="text">
<script>
$('#txtsdate').data('datepicker').setStartDate(new Date(TimezoneDate()));
$('#txtsdate').data('datepicker').setEndDate(Infinity);
$('#txtsdate').data('datepicker').setDate(sFulllDate);
</script>
until this point my code works well and i set normally the date. But when i try to get the date from the input and i am using
var sDate = $('#txtsdate').data('datepicker').getDate();
am getting 'Invalid Date'.
I have read this article https://github.com/uxsolutions/bootstrap-datepicker/issues/923
but doesn't seems to help me.
Try This Simply code to get input value of any text box using jquery
var sDate = $("#txtsdate").val();

Javascript. Add array to datetime input

I have a form with datetime_local html5 datetime input so that mobile users can use their native datetime picker instead of a jquery substitute which is generally clunkier.
I want the user to be able to select multiple dates.
So far I'm using the onchange trigger to capture the selected datetime and add it to an array. I then try and paste the array into the datetime input so it can be sent with the form.
With one date, it pastes fine, with more than one the input goes blank.
var datearray = [];
$(document).on('change','#mobile_gig_date',function(){
var date = $("#mobile_gig_date").val();
datearray.push(date);
console.log(datearray.join('; '))
$("#mobile_gig_date").val(datearray);
});
The HTML element is;
<input min="2017-09-01T00:00:00" discard_seconds="true" placeholder="Enter the gig date" id="mobile_gig_date" type="datetime-local" name="gig[date]">
Is there a way to add an array to a datetime input, and if so, in what format?
Since your input has the type datetime-local you can't put inside something that is not of that type.
Note that you also tried to put array inside an input - and you can't do this (what you can do is convert your array to string using JSON.stringify and use that value).
I added another hidden input and used it to save the data of your array.
var datearray = [];
$(document).on('change','#mobile_gig_date',function(){
var date = $("#mobile_gig_date").val();
datearray.push(date);
console.log(datearray.join('; '))
$("#mobile_gig_date_temp").val(JSON.stringify(datearray));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input min="2017-09-01T00:00:00" discard_seconds="true" placeholder="Enter the gig date" id="mobile_gig_date" type="datetime-local" name="gig[date]">
<input type="hidden" value="" name="gig[date_temp]" id="mobile_gig_date_temp" />

javascript - get the date value

I have a problem. I use a date picker in my html page which I want to get value of input date using jquery. I used this :
var birthday_date = $("#birthday_date").val();
but it's not working. I checked the inspect code then realise when I used date picker, the input tag (which used for date) becomes to this :
<input id="textbox1"
name="birthday_date"
data-targetselector="#textbox1"
class="form-control"
data-mddatetimepicker="true"
placeholder="year/month/day"
data-placement="top"
maxlength="10"
data-mdpdatetimepicker=""
data-trigger="click"
data-enabletimepicker="false"
data-mdformat="yyyy/MM/dd"
data-mdpdatetimepickerselecteddatetime="
{"Year":2011,"Month":8,"Day":2,"Hour":9,"Minute":13,"Second":14}"
data-original-title=""
title=""
data-mdpdatetimepickershowing="true"
aria-describedby="popover30621"
type="text">
the value of the date is this part:
mdpdatetimepickerselecteddatetime="{"Year":2011,"Month":8,"Day":2,"Hour":9,"Minute":13,"Second":14}"
the time is not matter, I just wanna date , google gave me nothing and I have no idea how can I get the value like this, any help? or something to help me?
If you are using datepicker, you can access the value like this:
var date = $('#textbox1').datepicker('getDate');

Passing value from JQuery Datepicker to code behind

I am using JQuery Datepicker in my .aspx file.
I need to use the date value in my code behind file. This is my function which I want to update a hidden value on my page which I can use in my code behind.
$(function () {
$("#datepicker").datepicker({ minDate: 0,
onSelect: function () {
var dueDate= document.getElementById('dueDate');
dueDate.value = $(this).datepicker('getDate');
}
});
});
My hidden value which I want to update, which is on the same .aspx page:
<Input id="dueDate" type="hidden" runat="server" />
Now in my code behind, I want to use the date like so:
DateTime due= dueDate.Value;
This gives me an error:
Cannot implicitly convert type 'string' to 'System.DateTime'
I get the same error when I use
DateTime due = Convert.ToDateTime(dueDate.Value);
What is the correct way to use the date from Datepicker in the code behind?
DateTime.Parse(...)
or
DateTime.ParseExact(...)
or
DateTime.Parse("01/01 2010");
or use
DateTime.TryParse
if you aren't sure it converts in which type every time, ie. not always a date, so try this 4 and check
Consider having the following code on your .aspx file, removing runat server:
<input type="hidden" id="dueDate" name="dueDate" value="" />
Now make the following change in your jquery datepicker function:
$(function () {
$("#datepicker").datepicker({
minDate: 0,
dateFormat: "dd-mm-yyyy",
onSelect: function() {
$("#dueDate").val() = $(this).datepicker("getDate");
}
});
}
This way the hidden field value of dueDate gets updated whenever the value of your datepicker control is changed. Further since your hidden field now has a name and value attribute associated with it, your code behind would receive its value as a string whenever your form is posted.
Now in your code behind file, create your DateTime object as follows:
string[] dueDateSplit = Request.Form["dueDate"].Split('-');
DateTime due = new DateTime(dueDateSplit[2], dueDateSplit[1], dueDateSplit[0]);
Provide a name for the datepicker
<Input id="dueDate" name = "dueDate" type="hidden" runat="server" />
and use the below
String text = Page.Request.Form["dueDate"]

CalendarExtender Change date with Javascript

I have a CalendarExtender control on my page and sometimes have to change the date to the next occuring Sunday. I'm currently using the OnClientDateSelectionChanged property of the control to call a function that will add some days to the date until its Sunday.
The problem I'm having is that if I select a Tuesday in my calendar, the textbox will display the next Sunday but the selected date in the calendar is still Tuesday.
How do I update the CalendarExtender to have the new date that has one I selected in javascript? The textbox the CalendarExtendar is connected to shows the correct date...
Changing the value of the textbox that is the TargetControlId for the CalendarExtender affects the selected date if the following 2 conditions are met:
An onchange event is fired on the textbox (either by changing the text manually or by calling an explicit javascript fireEvent() method.
The format of the date entered in the textbox matches the same format used by the CalendarExtender control.
That being said, the correct way to handle this is to call the set_selectedDate() function of the CalendarExtender control. This one call, not only sets the selected on the Calendar, but also on the Targeted textbox at the same time.
Here's the example code:
<cc1:CalendarExtender ID="CalendarExtender1" runat="server"
OnClientDateSelectionChanged="dateSelectionChanged"
TargetControlID="txtDate" PopupButtonID="imgCalendar">
</cc1:CalendarExtender>
<script type="text/javascript">
function dateSelectionChanged(sender, args){
selectedDate = sender.get_selectedDate();
/* replace this next line with your JS code to get the Sunday date */
sundayDate = getSundayDateUsingYourAlgorithm(selectedDate);
/* this sets the date on both the calendar and textbox */
sender.set_SelectedDate(sundayDate);
}
</script>
<asp:TextBox ID="txtDate" Text='<%# Bind("Date", "{0:dd-MMM-yyyy}") %>'
runat="server" class="form-control input-sm m-bot15" BackColor="#ffccbb"></asp:TextBox>
<asp:CalendarExtender ID="CalExtender1" runat="server" Enabled="true" Format="dd-MMM-yyyy"
TargetControlID="txtDate">
</asp:CalendarExtender>

Categories

Resources