This is a migrated thread and some comments may be shown as answers.

Hide time when midnight?

2 Answers 172 Views
Date/Time Pickers
This is a migrated thread and some comments may be shown as answers.
Sypher
Top achievements
Rank 1
Sypher asked on 08 Nov 2012, 09:17 PM
I'd like to have a datetimepicker that displays the date and time by default but only displays the date if the time is midnight. It should update the format when the value is changed. I can format the value when the datetimepicker is created to one or the other format, but if the time changes, I can't seem to update the picker's format. Any ideas on how to do this?

http://jsbin.com/ekilok/1/

$(document).ready(function () {
  var dateFormat = "{0:M/d/yy}";
  var datetimeFormat = "{0:M/d/yy h:mm tt}";
  var d = new Date(2012, 11, 8, 11, 20);
  var d2 = new Date(2012, 11, 8);
    
  $('#StartWithDate').kendoDateTimePicker({
      value: d,
      format: (d.getHours() === 0) ? dateFormat : datetimeFormat,
    change: function(e) { /* what goes here */ }
  });   
  $('#StartWithDateTime').kendoDateTimePicker({
      value: d2,
      format: (d2.getHours() === 0) ? dateFormat : datetimeFormat,
      change: function(e) { /* what goes here */ }
  });   
});

2 Answers, 1 is accepted

Sort by
0
Accepted
Dimo
Telerik team
answered on 09 Nov 2012, 01:38 PM
Hi Bryan,

The picker format can be changed after initialization via options.format. Since the widget has no mechanism to automatically refresh its display value after such a configuration change, you need to do that manually.

Note that I modified the dateFormat and datetimeFormat variable values.

var dateFormat = "M/d/yy";
var datetimeFormat = "M/d/yy h:mm tt";
var picker = $('#StartWithDate').data("kendoDateTimePicker");
var v = picker.value();
 
picker.options.format = datetimeFormat;
picker.element.val(kendo.toString(v, picker.options.format, picker.options.culture));


Regards,
Dimo
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Sypher
Top achievements
Rank 1
answered on 09 Nov 2012, 03:32 PM
Thanks! That was simple enough.

I'm using those date format strings across a whole application and they have to have the curly braces for grid column formats. It seems like the options.format field will take them either way, but setting the value with kendo.toString needs it to be without the braces. If I use kendo.format instead of kendo.toString to set the value, I can keep my global format strings. :-)
Tags
Date/Time Pickers
Asked by
Sypher
Top achievements
Rank 1
Answers by
Dimo
Telerik team
Sypher
Top achievements
Rank 1
Share this question
or