trentrichardson / jQuery-Timepicker-Addon

Adds a timepicker to jQueryUI Datepicker
http://trentrichardson.com/examples/timepicker/
MIT License
2.66k stars 1.05k forks source link

[Question] How to apply formating to variable? #862

Closed Eaglef90 closed 8 years ago

Eaglef90 commented 8 years ago

Hello, I was homing you might be able to help. I am trying to use your plugin to format the Date and time of a variable but all your examples are of formatting against a input box. Is it possible to pass in a JavaScript variable for formatting?

trentrichardson commented 8 years ago

You will need to use $.datepicker.formatTime():

var formatTime = $.datepicker.formatTime('HH:mm z', { hour: 14, minute: 36, timezone: '+2000' }, {});

There is an example of this near the bottom of the documentation examples page.

To format the date part you will need to use datepicker's method for formatDate() http://api.jqueryui.com/datepicker/

var formatDate = $.datepicker.formatDate( "yy-mm-dd", new Date( 2007, 1 - 1, 26 ) );

As you can see they are separate functions, but it is easy enough to concat the two strings.

Eaglef90 commented 8 years ago

Your assigning to a variable but I need to do the work against an existing variable. So if I already have a variable named "ApprovalDate" that is filled with a datetime from a database how do I pass that variable into your add-on for formatting?

trentrichardson commented 8 years ago

There is a function not listed in the examples called parseDateTimeString() that will return a js Date object:

$.datepicker.parseDateTime = function (dateFormat, timeFormat, dateTimeString, dateSettings, timeSettings)

OR If its a date string from the database that is recognized by js Date (MDN), then you can always just do:

var dateObj = new Date(ApprovalDate);
var dateStr = $.datepicker.formatDate( "yy-mm-dd", dateObj );
var timeStr = $.datepicker.formatTime('HH:mm', { hour: dateObj.getHours(), minute: dateObj.getMinutes() }, {});

var dateTimeStr = dateStr +' '+ timeStr;
Eaglef90 commented 8 years ago

Thanks that works, kind of. What formatting options can I use for formatTime? I need it in the equivalent of your gg:ii a setting (01:35 P.M.)

trentrichardson commented 8 years ago

"hh:mm TT" Check the docs for all the formatting options: http://trentrichardson.com/examples/timepicker/

Eaglef90 commented 8 years ago

Sorry got your formatting confused with another script I was testing out. Really appreciate the help!