5

As it can be seen on DateTimePicker / Basic usage, the example I used below sets the current hour 12:00 AM instead of DateTime.Now when pressing the link at the footer of DateTimePicker. How to fix it?

@(Html.Kendo().DateTimePickerFor(m => m.VisitDate)
    .Animation(true)
    .Format("dd/MM/yyyy HH:mm")
    .TimeFormat("HH:mm")
    .Min(new DateTime(1900, 1, 1)) 
    .Max(new DateTime(2099, 12, 31)) 
    .Footer(true)
    .Value(DateTime.Now) 
)

Before:
Before
After:
After

The link inserts midnight - but this feels wrong.

How do you make it insert the current time instead?


Update: Here is the DateTimePicker and javascript methods I used at the last step:

@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
    var today = DateTime.Now.ToString("dd/MM/yyyy 00:00", 
                    new System.Globalization.CultureInfo("en-US"));
}


@(Html.Kendo().DateTimePicker()
    .Name("datetimer")
    .Animation(true)
    //.Culture("en-US")
    .TimeFormat("HH:mm")
    .Min(new DateTime(1900, 1, 1)) 
    .Max(new DateTime(2099, 12, 31)) 
    .Value(DateTime.Now)
    .Format("dd/MM/yyyy HH:mm")
    .Events(e => e.Change("datetimepicker_change"))
)


<script>
function datetimepicker_change() {
    // I use this method so that when selecting another day except from today, 
    // the hour should be 00:00. But it does not make sense whether or not using it 
    if ($('#datetimer').val() != '@today') {
        return;
    }


    if ($('#datetimer').val() == '@today') {
        $('#datetimer').val('@DateTime.Now.ToString("dd/MM/yyyy HH:mm")');
    }
}
</script>
Jack
  • 1
  • 21
  • 118
  • 236

1 Answers1

4

Hello this is how i handle this;

var today = DateTime.Now.ToString("dd/MM/yyyy 00:00", new System.Globalization.CultureInfo("en-US"));

@(Html.Kendo().DateTimePicker()
    .Name("test")
    .Animation(true)
    .TimeFormat("HH:mm")
    .Min(new DateTime(1900, 1, 1))
    .Max(new DateTime(2099, 12, 31))
    .Value(DateTime.Now)
    .Format("dd/MM/yyyy HH:mm")
    .Events(e => e.Change("datetimepicker_change"))
 )

 <script>
  function datetimepicker_change() {
    if ($('#test').val() == '@today') {
        $('#test').val('@DateTime.Now');
    }
  }
 </script>

full code via VS 2015

result : youtube

Zergling
  • 526
  • 2
  • 7
  • I encounter "SyntaxError: missing ) after argument list: $('#test').val(23/12/2015 16:46:00); Note that it is an MVC project and I use this method on Razor page. I removed some " ' " sign, but this time I get the error above. Any idea? You can test it on http://dojo.telerik.com/ – Jack Dec 23 '15 at 14:48
  • that code i build with razor syntax already just make it as i do – Zergling Dec 23 '15 at 14:57
  • Sorry, but for each case I encounter an error (compliation, etc.). I defined today variable just before DateTimePicker and datetimepicker_change() after DateTimePicker. I also tried to define them in – Jack Dec 23 '15 at 15:28
  • @Christof i cannot use kendo mvc on dojo , so all you need is here. updated ; i record it aswell watch link at the end. – Zergling Dec 23 '15 at 15:53
  • What about setting hour 00:00 when selecting another day (except from today)? Because when selecting another day, the hour still the same as the last selected hour and minute. Any idea? – Jack Dec 23 '15 at 16:29
  • Ok, I found it. In that case we add this line before the first if condition: `if ($('#test').val() != '@today') { return; }` – Jack Dec 23 '15 at 16:38
  • But it is not working properly. If you have time could you pls try this if condition by selecting today and then selecting another date etc. After first work, it keep the previous time and not change. Any idea? – Jack Dec 23 '15 at 17:11
  • i'll work on it at work tomorrow , i dont have kendo mvc dlls right now – Zergling Dec 23 '15 at 17:29
  • its works well i just keep switching dates result same all works – Zergling Dec 24 '15 at 07:46
  • The scenario is like that: 1) The first time value is 24.12.2015 10:01. 2) When selecting another date except from today it is also ok > 23/12/2015 00:00. 3) But, then selecting today again the value is 24/12/2015 00:00 whereas it should be 24/12/2015 10:01. Any idea? – Jack Dec 24 '15 at 08:03
  • are u selecting today on footer at third step ? – Zergling Dec 24 '15 at 08:09
  • Yes, in both cases (selecting on footer or the day number) the result is the same as I described above. – Jack Dec 24 '15 at 08:13
  • can u watch this there is no problem for me. https://www.youtube.com/watch?v=r7micL2MBYM&feature=youtu.be – Zergling Dec 24 '15 at 08:20
  • remove this line if ($('#datetimer').val() != '@today') {return;} – Zergling Dec 24 '15 at 09:41
  • As I indicated on the line, it does not make sense and when selecting another date, the hour is the same as the last selected hour i.e. 11:30) instead of 00:00. I think that might be the version problem of Kendo, but if you have an idea to change the hour of the selected date to 00:00 I can apply. If not, I do not want to spend your time much more. Because you already solved the problem and proof it. Thanks... – Jack Dec 24 '15 at 09:47