Jquery Datepicker Set Value From String - javascript

I am getting a string date from cookie which returns like this format 2017-11-16. My datepicker dateformat is D, M d, yy e.g Thu, Apr 27, 2017.
if I just set it as $("#m_checkin").datepicker().datepicker("setDate", "2017-11-16"); datepicker functions stop working and it just set 2017-11-16 as an input value. How can I format my date to my datepicker dateformat?
https://jsfiddle.net/n6dokkty/

You need to parse date before set date to particular format.
You can see here: https://jsfiddle.net/r1nm5h7k/
$( "#datepicker" ).datepicker({dateFormat: "DD, d MM, yy"});
$( "#datepicker" ).datepicker("setDate", $.datepicker.parseDate( "yy-mm-dd", "2017-11-16" ));
ref : https://api.jqueryui.com/datepicker/#utility-formatDate

You could look into momentjs, which is a javascript date/time library. With that, you can easily convert between dates of different format.
string newDate = moment(currentDate, currentFormatString).format(newFormatString)

You have to setDate with Date object instead of string (to avoid date format issues like you face currently).
So you have to change your code like below:
$("#m_checkin").datepicker().datepicker("setDate", new Date(2017,11,16));

Related

moment format verbose date as date object

I used materialize datepicker to pick a date in french format. Now I need this date formatted back to a date object so I can use it in my api. Here's how I try to convert the date back to a normal format:
moment("dimanche 30 juillet 2017","dddd D MMMM YYYY").locale('fr').toDate();
But I receive Invalid Date. Is there a way to convert this date back using moment? Or can I somehow hook to materialize component to retrieve a normal date?
You need to set the fr locale before attempting to parse french day/monthnames.
moment.locale('fr');
moment("dimanche 30 juillet 2017","dddd D MMMM YYYY").toDate();
You can parse your input string passing locale parameter, see moment(String, String, String) docs:
As of version 2.0.0, a locale key can be passed as the third parameter to moment() and moment.utc().
Here a working sample:
var m = moment("dimanche 30 juillet 2017", "dddd D MMMM YYYY", 'fr');
console.log(m.toDate());
console.log(m.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js"></script>
For further info see Changing locale globally and Changing locales locally.

Json - Date not showing in correct format

When i fetch date from the db it is in following format :-
{5/13/2002 12:00:00 AM}
When i pass this as Json, i bind it to textbox & it shows me like this :-
/Date(1021228200000)/
How to display date in any correct format?
Extending this question I want to bind the date to html5 datepicker, How to do this?
var jsonDate = "/Date(1021228200000)/";
var date = new Date(parseInt(jsonDate.substr(6)));
The substr function takes out the "/Date(" part, and the parseInt function gets the integer and ignores the ")/" at the end. The resulting number is passed into the Date constructor.
jQuery dateFormat is a separate plugin. You need to load that explicitly.
Reference
You can use like below to display datetime and bind date to html5 datepicker.
<input class='selector' id='datepicker'/>
$( ".selector" ).datepicker({ dateFormat: 'yy-mm-dd' }).val();

Convert Any Date Inserted To ISO Format Without The Timestamp In JavaScript

I want to know how can convert any date that entered to text box using JQuery Datepicker to the ISO date format without the Timestamp. At the below you can find the code sample that I have used.
$("#to_date").datepicker({ maxDate: new Date(),dateFormat: 'yy-mm-dd'});
$("#from_date").datepicker({ maxDate: new Date(),dateFormat: 'yy-mm-dd'});
But when I pass these dates to a JSON web service it shows is invalid date. So what should I do?
Thanks & regards,
Chiranthaka
From original documentation:
Display the date in ISO format. Produces "2007-01-26".
$.datepicker.formatDate( "yy-mm-dd", new Date( 2007, 1 - 1, 26 ) );

Get local datetime format In Jquery

I want to get local machine datetime format in Jquery?
means if local date time format is in "MM-dd-yyyy" ,so this format I want to get in jquery.
OR
How can I convert my any datetime in local datetime format in jquery?
You can change date format in jquery datepicker as such:
var date = $('#datepicker').datepicker({ dateFormat: 'dd-mm-yy' }).val();
This Link , Link and Link may be helpful.

How to format datetime with the datetimepicker plugin?

What I want is a format like this:
Thu , 13 March 2013 03:28pm
Here is what I've tried so far:
$('.datetimepicker').datepicker({
dateFormat : 'D , d M yy hh:mm'
});
This is the result I got:
Thu , 13 March 2013 hh:03
What can I do to get the desired format?
See this: Fiddle
Reference Link
$('.datetimepicker').datetimepicker();
I don't think you can achieve it since dateFormat is just for datepicker, you need to achieve it through timepicker as well. Here is a blog stated how you can obtain it:
http://trentrichardson.com/examples/timepicker/
Out of the box the jQuery UI datepicker does not provide the time as a possible format. See the documentation.
use the following code:
$('.datetimepicker').datepicker({
dateFormat: 'D , d M yy', timeFormat: 'hh:mm TT'
});
In it small m is represent the numeric month.
Above code will work for you.
For reference check this link.

Categories

Resources