javascript - get the date value - javascript

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');

Related

Can't open date picker using .click() in protractor > "Failed: element not interactable"

When I try to trigger a date picker, an error is shown 'Failed: element not interactable'
Structure of an element is the following
<div class="datepicker-input-wrapper datepicker-input-wrapper-start">
<div class="datepicker-trigger"></div>
<input type="text" class="datepicker-input datepicker-input-start" autocomplete="off">
<span class="">Anreise</span>
</div>
My code is:
var start_date = element(by.className('datepicker-trigger'));
start_date.click();
I expect to see date picker opened
There are no other locators except classes, that's why I used by.className
Tried to maximize browser window or scroll to element, it didn't help
Maybe it would be easier if you just use the input with sendKeys like this:
element(by.css('.datepicker-input.datepicker-input-start')).sendKeys('DD-MM-YYYY');
Or whatever date format it accepts

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" />

How to set defaultDate in local time using a UTC value from a hidden field in eonasdan-datetimepicker?

I am populating a hidden field with a UTC date/time value from a database. I am then using some jQuery and the moment library to convert the UTC date to local time and set the default date of a eonasdan-datetimepicker. I am also using the dp.change event of eonasdan-datetimepicker to convert dates that are picked via the widget back to UTC to bind back to the database via the hidden field. Here is my javascript - this works, but I do not like it.
$(".datepicker").each(function (index) {
var utcDateTime = moment.utc($("#" + $(this).data("utc-target")).val(), "MM/DD/YYYY h:mm A");
var localDateTime = moment.utc(utcDateTime).local().format("MM/DD/YYYY h:mm A");
$(this).find(">:first-child").val(localDateTime);
}).datetimepicker({
useCurrent: false,
}).on("dp.change", function (e) {
$("#" + $(this).data("utc-target")).val(moment.utc(e.date).format("MM/DD/YYYY h:mm A"));
});
HTML:
<label for="PublishedField">Date/Time Published</label>
<div class="input-group date datepicker" data-utc-target="PublishedUtcField">
<input name="PublishedField" type="text" id="PublishedField" class="form-control" />
<span class="input-group-addon"><span class="fa fa-calendar"> </span></span>
</div>
<input type="hidden" name="PublishedUtcField" id="PublishedUtcField" value="4/10/2015 5:16:00 PM" />
While this works, I'm not a fan of the each loop to set the initial value of the datetimepicker. I feel as though it would be cleaner to set it via the defaultDate property, but I am unable to set that property using an anonymous function. I think a dp.initialize or dp.setup event would be a nice solution, but the library does not support something like that, as far as I can tell. This could also be done using some kind of placeholder functionality, but I cannot find any similar scenarios in the docs or on GitHub.
Anybody know of a better way to do this? Is this kind of scenario supported somehow in a way I'm just not seeing or aware of? Am I missing something obvious?

how to show value of textbox type=date asp.net

I have a textbox with the property type="date". I am able to retrieve the value in backcode but when I try to assign a value, the placeholder "mm/dd/yyyy" is shown.
When I view the markup, the expected value is there - "value='01/01/1991'". I think it has to do with JavaScript overriding the value.
Any suggestion on how I can work around this?
Edit: Sorry, forgot to include the markup and code
So my markup is
<asp:TextBox ID="txtBirthdate" runat="server" class="form-control input-sm" type="date" onkeypress="handleEnter(event)" />
and c# is
txtBirthdate.Text = usr.BirthDate.ToString("MM/dd/yyyy");
the resulting markup is
<input name="ctl00$ctl00$ctl00$ctl00$Body$Body$Body$Body$txtBirthdate" value="01/20/1991" id="ctl00_ctl00_ctl00_ctl00_Body_Body_Body_Body_txtBirthdate" class="form-control input-sm" type="date" onkeypress="handleEnter(event)">
So as you can see, the value was assigned.
I believe this only happens in Chrome. It requires value assignment to be in "yyyy-MM-dd" format. Try in your C#
txtBirthdate.Text = usr.BirthDate.ToString("yyyy-MM-dd");
This should display the correct value (and it doesn't affect the format of the display).
If will display in this format in other browsers tho. You may want to do some browser detection, e.g.
if (Request.Browser.Browser == "Chrome")
{
txtBirthdate.Text = usr.BirthDate.ToString("yyyy-MM-dd");
} else {
txtBirthdate.Text = usr.BirthDate.ToString("MM/dd/yyyy");
}
but perhaps something more sophisticated
For me this worked:
txtBirthdate.Text = Convert.ToDateTime(TablaHabitacion.Rows[fila].Cells[6].Text).ToString("yyyy-MM-dd");

Categories

Resources