bootstrap-datetimepicker
bootstrap-datetimepicker copied to clipboard
Datetimepicker sometimes resets to 12/31/1899 12:00 AM
I'm using datetimepicker but I do not want to pick the minutes.
I've set minView
to 1 but the minutes still appear as something other than 00
.
What I did then was set the format string to "mm/dd/yyyy HH:00 P"
. All good except for when the user clicks outside the picker without specifying a new date.
I've created a fiddle so you can try it: http://jsfiddle.net/onumis/MgxeR/5/
It uses your latest JS and CSS, Twitter Bootrap 2.3.2, and jQuery 1.10.1. (Note that my JS code is at the bottom)
Am I doing something wrong or is it the library that is not parsing/incompatible with the provided format string?
This seems to happen to me whenever I use the meridian (am / pm), especially when I use a space between the minutes and am/pm. No consistency to reproduce though.
Updated: http://jsfiddle.net/MgxeR/6/
Got it working for this very specific case, it might have broken something, but from my 5 minute manual testing, all is fine.
I've changed the update
function (line 465 on the fiddle):
if (typeof date == 'string' || date instanceof String) {
date = date.replace(/^\s+|\s+$/g,'');
}
to
if (typeof date == 'string' || date instanceof String) {
date = new Date(date.replace(/^\s+|\s+$/g,''));
}
I noticed the problem was that DPGlobal.parseDate
could not parse the date (as a string, with the given format), and ended up setting date to new Date(0, 0, 0, 0, 0, 0, 0)
.
This function is quite confusing, so I'll leave the hard work to someone else ;)
Hmm. This gives me errors. Glad it worked for you though!
If I get any time I might do a pull and see if I can fix the whole thing...
I'm continuing to get this error - and the fix above only breaks the plugin entirely.
It seems to entirely come down to the format, and sometimes switches to 1899 if you activate the dropdown, and click away, and sometimes only switches when you select a date.
Any format without a time breaks it and sets it to 1899 as the plugin cannot parse the date. This is quite annoying, as I had hoped to use this one plugin to enable both date and datetime usage to save on code.
Ex:
$('input.date[data-range="single"]').datetimepicker({
minView: 2,
format: 'dd-mm-yyyy',
startDate: new Date,
autoclose: true,
});
Breaks. But:
$('input.date[data-range="single"]').datetimepicker({
minView: 2,
format: 'dd-mm-yyyy HH:ii p',
startDate: new Date,
autoclose: true,
});
Works perfectly. The same can also be done by removing the space between i and p - if the format is HH:iip then it also gets confused when no date is selected.
I think a lot of this can be simplified by only running the date through the parser for output when a date has actually been selected, NOT when the user clicks off the dropdown.
I was having a similar problem. If I re-clicked the control I added after I set the date and time, I was set to year 1899 after that.
My original format was yyyy-mm-dd HH:ii:00 P.
I removed the space between the time and Postmeridian, like so: yyyy-mm-dd HH:ii:00P, and now everything seems to be working find. I can re-click the field and change values as long as I wish to.
@ma077146 solution worked for me! 👍
I had to tweak the parseDate function in boostrap-datetimepicker.js to make it work properly. Currently the date is set to:
date = new Date(0, 0, 0, 0, 0, 0, 0), // which is Dec. 31, 1899
change to:
date = new Date(), // which is today's date
I also had this issue and it was certainly annoying. I needed to ensure that all date formats that were being fed to the input control, as well as any dates being returned from datetimepicker were all formatted exactly the same way. For me, given my locale I used "dd/mm/yyyy HH:ii".
This format was also replicated on the SQL Query which was used to initially supply the stored date: CONVERT(VARCHAR(10), tblAppointment.StartDateTime, 103) + ' ' + CONVERT(VARCHAR(5), tblAppointment.StartDateTime, 108) AS StartDateTime)
We also encountered a similar issue on our project, and we fixed it without touching the plugin code. I wrote about it here: https://oprea.rocks/blog/bootstrap-datetimepicker-reset
Just skip past my crude storytelling and go to the middle of the article where the actual solution is explained.
Can't make it work. I want my datetime to be shown as like 14 May 2018 7:12pm If I use this format "dd M yyyy HH:ii" it works but as soon as I add "p" by the end it resets to weird 1899 date.
@proximadata That's probably because it's unable to parse your time properly. What does your full format look? Besides "HH:ii" you probably have your timezone... you need to separate it with a space, I believe, from the time, so the library can properly parse that format.