-2

Hello I am using maxDate in datetimepicker like this:

const today = new Date()
$('#recordtime').datetimepicker({
   sideBySide: true,
   locale: 'bg',
   format: 'DD/MM/YYYY  HH:mm:ss',
   maxDate: today
});

But the problem is that I can pick till 8:00:00 - today enter image description here

Why is That? Do I need to set some local time settings in the script to restrict to 00:00:00?

The js that I use is: https://cdnjs.com/libraries/bootstrap-datetimepicker/4.17.37

Hristian Yordanov
  • 650
  • 1
  • 6
  • 25
  • What is `datetimepicker`? What does the documentation of that plugin say about the `maxDate` property? Did you try anything yourself to solve this (like setting the minutes and hours to zero)? – Andreas Mar 23 '22 at 08:50
  • @Andreas I cant find any info about this problem... – Hristian Yordanov Mar 23 '22 at 08:51
  • That doesn't answer any of my questions... – Andreas Mar 23 '22 at 08:52
  • @Andreas this is `bootstrap-datepicker` plugin – Hristian Yordanov Mar 23 '22 at 08:54
  • @Rederdex `const today = new Date()` is the first line of OPs code... – freedomn-m Mar 23 '22 at 08:55
  • @Rederdex now here is 10:55. And the 8:00:00 restriction is the same – Hristian Yordanov Mar 23 '22 at 08:55
  • For boostrap-datepicker, use `endDate` with `"today"` (a string) or `"0d"` as in the example. https://bootstrap-datepicker.readthedocs.io/en/latest/options.html#enddate – freedomn-m Mar 23 '22 at 08:56
  • 1
    The linked documentation has only the `format` option of your config. Either its not the bootstrap datepicker, or your config is wrong. – Andreas Mar 23 '22 at 08:57
  • Also, if "maxdate" is today, then today is allowed – freedomn-m Mar 23 '22 at 08:57
  • 1
    This feels like a time zone issue. Are you, by any chance, in a GMT +/- 8 timezone? – phuzi Mar 23 '22 at 08:58
  • As noted by @Andreas above, https://bootstrap-datepicker.readthedocs.io/en/latest/ is a date picker, it doesn't include *time* - so it's not the "datetimepicker" that you're using. Please let us know the actual date+time picker. – freedomn-m Mar 23 '22 at 08:58
  • If you don't want "today" then set the max date to "yesterday". Or, if it has a time, then yesterday 23:59 - or today 00:00 - `new Date()` in javascript includes the current time. The datepicker/datetimepicker *should* have a concept of "maxDate today" without needing to pass in a date object - if you told us the correct plugin, we could possibly help you find it. – freedomn-m Mar 23 '22 at 09:00
  • 1
    Sorry the js that I use is made by: bootstrap-datetimejs https://github.com/Eonasdan/bootstrap-datetimepicker Copyright (c) 2015 Jonathan Peterson – Hristian Yordanov Mar 23 '22 at 09:01
  • `new Date()` includes time, so you're setting max to "now" rather than "beginning of today". It's unclear from "*But the problem is that I can pick till 8:00:00 - today*" if you're expecting to be able to select any time today or not today at all. Seems the easiest solution is to set hours/minutes to zero on your `today` variable: `today.setHours(0,0,0,0)` see https://stackoverflow.com/a/6202196/2181514 and related answers regarding potential timezone issues - probably not an issue for a datetimepicker. – freedomn-m Mar 23 '22 at 09:08
  • @freedomn-m you are right, still the main problem now I see that the time is 09:00:00, instead of 08. I think now that timezone is the problem like phuzi says – Hristian Yordanov Mar 23 '22 at 09:17

1 Answers1

0

The solution was to set the time to today.setHours(23,59,59,0) like @freedomn-m said and add timeZone as @phuzi wrote.

const today = new Date()
today.setHours(23,59,59,0)
$('#recordtime').datetimepicker({
locale: 'bg',
format: 'DD/MM/YYYY  HH:mm:ss',
timeZone: 'Europe/Sofia',
maxDate: today
});

Thanks for the help in the comments and sorry for the wrong info that I wrote for the used js script...

Hristian Yordanov
  • 650
  • 1
  • 6
  • 25