views:

14382

answers:

9

I have a Datepicker and a Timepicker in 2 separate input fields but I need to combine the 2 fields inputs into one for a database call. Wanted to know if I could only use 1 input field for both controls?

first call the datepicker and the user would click the date wanted and it would enter the value, then the timepicker would popup and append the date the user selected in the first action.

I'm using the jQuery UI Datepicker and Timepickr plug-ins

So I have something like this

<input class="datepicker">2009-06-22</input>
<input class="timepicker">12:00:00</input>

But would like something like this

<input class="datetimepicker">2009-06-22 12:00:00</input>

<script type="text/javascript">
// Date and Time picker same input field
$("#datepicker").datepicker( {
   dateFormat: 'yy-mm-dd',
   onClose: function(dateText, inst) {
      var tempDate = $("#datepicker").val(dateText);
      $("#datepicker").timepickr({
         onClose: function(timeText, inst) {
            var tempTime = $("#datepicker").val(dateText);  
            alert("Temp Time: '" + tempTime.val() + "'");
            $("#datepicker").val(tempDate.val() + ' ' + tempTime.val());
         }  
      });           
   }
});             
</script>
+2  A: 

Here's an alternative:

http://www.r-ip.ru/dev/datetimepicker/index.html

$('#datetimepicker').datetimepicker({ 
  dateFormat: 'yy-mm-dd',
  timeFormat: ' hh:ii:ss' 
});
Alexander Corotchi
The Date Time picker on that page has what the author asked for...
Shadi Almosri
That wasn't stated. Also, the interface for the time-picker on the provided link is terrible. Took me a few seconds to figure out that clicking a value increments it. Either way, I've removed my down-vote.
Jonathan Sampson
I agree it has what I'ved asked for but not with the plug-ins I'm using which I would like to use. The solution you provided is good but I would agree the the time controls are a little confusing. Thanks though
Phill Pafford
The link for that is now dead.
Marcel Tjandraatmadja
A: 

You could bind an event to the onClose() event of the datepicker. Have it open up your time picker. Let the datepicker write the base value of the input, and have the timepicker append it's value following a space to the same input.

  1. Bind date-picker to input.
  2. Attach method to open timepicker to .onClose() event of date-picker.
  3. Append time-picker value (with a preceding space) to input value.

Updated: The follow is an updated resulting from an updated question.

The documentation gives a brief example on how to bind an event/action to the closing of the datepicker:

$('.selector').datepicker({
   onClose: function(dateText, inst) { ... }
});

In this example, you could use your function following "onClode: " to write the value to the input, and popup the timepicker.

$(".selector").datepicker({
  onClose: function(dateText, inst) {
    $(".selector").val(dateText);
    // Raise Timepickr
  }
});
Jonathan Sampson
I like the flow of this option but have never binded anything. I have edited my post above to show you the code I'm using. Very basic
Phill Pafford
hmm how do I call the timepicker?
Phill Pafford
I'm sorry - I don't know. I've never used that particular 3rd party plugin. You would have to consult the documentation for it, or contact the plugin author. In all honesty, it may be easier to seek out a different 3rd party plugin that does both date and time built into one - something like http://razum.si/jQuery-calendar/TimeCalendar.html or the plugin mentioned in Alexander's response.
Jonathan Sampson
You could try to bind that plugin to the $(".selector") after the $(".selector").val(dateText); line, and then invoke the .click() or .focus() method of the $(".selector") immediately afterwards. That may do it.
Jonathan Sampson
I have change the code some but still having issues, see new edit
Phill Pafford
A: 

You can try combining the inputs from both controls into one like

$("#datetime").focus(function() {
 var mydate = $("#date").val();
 var mytime = $("#time").val();
 if(mydate && mytime && !this.value) {
  this.value = mydate + " " + mytime;
 }
});
dhaval
hmm interesting, thanks
Phill Pafford
A: 

Found this link:

http://milesich.com/timepicker/

Just want I was looking for, thanks all for your input ;)

Phill Pafford
The link is broken.
Juha Syrjälä
Sorry not my site, it was a great plugin though. Maybe you could Google for the code somewhere, I will tried a quick search for the URL but no go
Phill Pafford
http://our.umbraco.org/projects/backoffice-extensions/skiltzdatetimepicker try this
Phill Pafford
A: 

I've had need to address this problme myself recently. My solution has been to modify a plugin for jQuery-UI.

I've an example http://puna.net.nz/timepicker.htm'>here.

Fentex
+2  A: 

Ah, sod. I screwed up entering the link there, here it is again...

jQuery Timepicker mod

Fentex
A: 

Hi, Can you help me to implement this code http://home.jongsma.org/software/js/datepicker

I don't know how to use it I need a complete example pleaze Help me I really need this

Thanks a lot

Katy
You should post your own question about this, lots of people would love to help :)
Phill Pafford
+3  A: 

I'd like to recommend Any+Time(TM), in which you can combine the date/time in a single field with a single, easy-to-use (and quick!) date/time combo picker.

Andrew M. Andrews III
Thanks this is a nice addition
Phill Pafford
Version 4 has just been released, with many new features including support for TIME ZONES (try to find THAT in any other picker!), jQuery selectors/chaining and easy-removal (to reduce memory leaks and support more dynamic pages). Visit http://www.ama3.com/anytime/ to learn more and download the code. Please use the contact link on that site if you experience any problems or have any suggestions. Thanks!
Andrew M. Andrews III
A: 

please checkout the link

http://pastebin.com/kTfDz4km

I have updated some layout issue and added second slider , which will be active if {showTime:true, time24h:true}

you can fine the original one at :

http://code.google.com/p/php-form-builder-class/source/browse/trunk/includes/jquery/ui/timepicker.js?spec=svn176&amp;r=176

sakhunzai