monim67 / django-bootstrap-datepicker-plus

Bootstrap3/Bootstrap4/Bootstrap5 DatePickerInput, TimePickerInput, DateTimePickerInput, MonthPickerInput, YearPickerInput with date-range-picker functionality for django >= 2.0
https://pypi.python.org/pypi/django-bootstrap-datepicker-plus
MIT License
223 stars 63 forks source link

How to use additional date-time formats e.g "%d/%m/%Y" #15

Closed MaximeVlnve closed 5 years ago

MaximeVlnve commented 5 years ago

Hi,

I am currently trying to use your datetime picker and first of all i want to thank you about this useful component.

Secondly, i am here because i am french and i want to format the datetimepicker according to my locale. For that, i add to my DateTimePickerInput a format so that i have :

DateTimePickerInput(format="%d/%m/%Y %H:%M")

Basically i just wanted to invert month and day.

Also, in that case that does not work well but when i do not use a format option such as mentionned previously, your component works well (the datetime i wanted to save in my db is saved successfully).

Thanks in advance for your time,

Maxime

monim67 commented 5 years ago

I can help you with that, post the code of your form.

MaximeVlnve commented 5 years ago

Hi, thanks in advance, here is the simple code :

    class Meta:
        model = Appointment
        fields = ['label', 'start', 'end']
        widgets = {
            'start': DateTimePickerInput().start_of('app'),
            'end': DateTimePickerInput().end_of('app'),
        }
monim67 commented 5 years ago

These are the date formats django accepts by default:

['%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', '%b %d %Y', '%b %d, %Y', '%d %b %Y', '%d %b, %Y', '%B %d %Y', '%B %d, %Y', '%d %B %Y', '%d %B, %Y']

These are the date-time formats django accepts by default:

['%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M:%S.%f', '%Y-%m-%d %H:%M', '%Y-%m-%d', '%m/%d/%Y %H:%M:%S', '%m/%d/%Y %H:%M:%S.%f', '%m/%d/%Y %H:%M', '%m/%d/%Y', '%m/%d/%y %H:%M:%S', '%m/%d/%y %H:%M:%S.%f', '%m/%d/%y %H:%M', '%m/%d/%y']

If you need a format not listed here, a widget has nothing to do unless you specify it on the from field passing a input_formats parameter.

class YourForm(forms.ModelForm):
    start = forms.DateTimeField(input_formats=["%d/%m/%Y %H:%M"], widget=DatePickerInput(format="%d/%m/%Y %H:%M").start_of('app'))
    end = forms.DateTimeField(input_formats=["%d/%m/%Y %H:%M"], widget=DatePickerInput(format="%d/%m/%Y %H:%M").start_of('app'))
    class Meta:
        model = Appointment
        fields = ['label', 'start', 'end']
MaximeVlnve commented 5 years ago

Oh ok, missed that info about date-time formats accepted by default. Thanks a lot for your explanation and solution.

monim67 commented 5 years ago

You didn't miss that, I never mentioned it in the docs. Thanks for bringing it up, will include it in the docs next time.

pedrofaria09 commented 5 years ago

Please include this on the docs 👍 I have lost some hours searching for this solution.

matthpau commented 5 years ago

May I ask what start_of('app') means in the above items?

monim67 commented 5 years ago

@matthpau please follow the readme to see how to use the start_of/end_of methods, and if you have further queries open a separate issue about that.

monim67 commented 2 years ago

Documentation updated to address this issue #48.