2

This is my bootstrap input field,

<input class="form-control" id="date" required="required" name="date" type="date">

I want to set date field value after some event using jquery,

// this is a date string return from database
var data = 2019-05-06 00:00:00 ;
$('#date').val(data);

but it's not set the value in bootstrap date picker.

2 Answers 2

3

You can use datepicker update

Update the datepicker with given arguments or the current input value. The arguments can be either an array of strings, an array of Date objects, multiples strings or multiples Date objects. If date arguments are provided and they are Date objects, it is assumed to be “local” Date objects, and will be converted to UTC for internal use.

Sample:

var data = "2019-05-06 00:00:00";
//data is a string
$(".datepicker").datepicker("update", data);

UPDATE Setting up a date type html

var data = "2019-05-06 00:00:00";
document.getElementById("date").valueAsDate = new Date(data);
<input class="form-control" id="date" required="required" name="date" type="date">

Sign up to request clarification or add additional context in comments.

7 Comments

I really don't want to use datepicker library. I wanted to use bootstrap built in date field. can you tell me why my solution doesn't work ?
This is from bootstrap @Md.SukelAli
If you are trying to setup value for html date type, I added a sample @Md.SukelAli
Yes ! I am looking for this solution.
I think jQuery val() is setting the value of the DOM. We need to set valueAsDate for date.
|
0

You can try

var data = '2019-05-06 00:00:00';

$('#date').datepicker("update", data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.