bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

Datepicker shows one day behind when using yyyy-MM-dd format

Open yamakuzuryu opened this issue 12 years ago • 23 comments

screen shot 2014-08-25 at 9 41 29 am

yamakuzuryu avatar Aug 25 '14 15:08 yamakuzuryu

+1, can confirm.

kz26 avatar Aug 27 '14 20:08 kz26

I have the same issue, any solutions?

charger513 avatar Sep 04 '14 17:09 charger513

This is probably a time zone issue, try displaying model.toISOString() to see.

awb avatar Sep 09 '14 22:09 awb

@awb You are right :+1: . I could solve this adding timezone with getTimezoneOffset() :)

charger513 avatar Sep 09 '14 22:09 charger513

So is this fixable on my end or is this a bug within the directive? I'm passing a date like "2014-09-10" and the datepicker dropdown is displaying 2014-09-09 but the input is correct.

yamakuzuryu avatar Sep 10 '14 06:09 yamakuzuryu

I fixed it simply by converting all date strings (my backend returns ISO dates like 2014-09-10) into date objects in my controller.

kz26 avatar Sep 10 '14 13:09 kz26

This is how I fixed it. I added my timezone

var d = new Date('yourDate'); d.setMinutes( d.getMinutes() + d.getTimezoneOffset() );

'd' should be the correct date

charger513 avatar Sep 10 '14 14:09 charger513

Please put this in the docs - I just realized this as well (too late).

For yyyy-MM-dd it does not make sense, since the user is not entering a time value, so time is always T00:00:00 (+/- the gmt offset when converted to string)

mgaerber avatar Oct 17 '14 14:10 mgaerber

This should now be addressed in 0.14.3.

There are still some timezone issues, i.e. the weakness in not being able to configure it, but one can set the timezone used by the datepicker via setting an initial model date value with the appropriate timezone.

wesleycho avatar Oct 30 '15 14:10 wesleycho

This is still not fixed.

format: {
    toDisplay: function(date, format, language)
    {
        console.log(date);
    },
toValue: function(date, format, language)
    {
        return date;
    }
}

This outputs:

Tue Sep 06 2016 20:00:00 GMT-0400

Today is the 7th!

shrimpwagon avatar Sep 07 '16 21:09 shrimpwagon

I guess, I am experiencing the same issue right now, version 1.3.3

charlie-wasp avatar Oct 04 '16 05:10 charlie-wasp

I'm experiencing this same issue, and I'm not even concerned with the time of day. I just want to be able to choose a day. Picker keeps picking the day before. Not a good outcome for me.

DavidRogersDev avatar Oct 14 '16 06:10 DavidRogersDev

I ran into this yesterday and found the solution is to set the timezone option to utc and the date is no longer off by one. The syntax for doing that is here: https://github.com/angular-ui/bootstrap/issues/4837#issuecomment-203284205

elijahlofgren avatar Oct 14 '16 11:10 elijahlofgren

Thanks Elijah. But that did not resolve the issue for me. I really wish they would fix this bug, rather than just close the many Issues that people raise which reference it.

DavidRogersDev avatar Oct 16 '16 22:10 DavidRogersDev

If you'd like to hardcode a timezone, you could do what I did. As @charger513 mentioned, you can set the minutes along with the timezone offset -- but instead of determining your current timezone automatically, you could substitute the timezone offset with the amount of minutes - in my case, my local timezone was returning -660 (UTC-11). But I needed it to be 480 (UTC+08).

var d = new Date('yourDate');
d.setMinutes( d.getMinutes() + 480 );

I was then finally getting the output I wanted - as my PHP backend counterpart was aligned to the UTC+08 timezone.

Find your timezone minutes offset here: https://forbrains.co.uk/international_tools/earth_timezones

gbrits avatar Mar 13 '17 11:03 gbrits

I have found a way. convert that date to string first. here is the code.

 var SelectDate = $scope.GetFormattedDate(Date.parse($("#Date").datepicker("getDate")));            
     $scope.GetFormattedDate = function (CalDate) {
            var re = /-?\d+/;
            var WDate = CalDate.toString();
            var m = re.exec(WDate);
            var lastDate = new Date(parseInt(m[0]));
            var mm = lastDate.getMonth() + 1;
            var dd = lastDate.getDate();
            var yyyy = lastDate.getFullYear();
            var formattedDate = mm + '/' + dd + '/' + yyyy;
    
            return formattedDate;
        }

Now Pass SelectDate to your controller. bingo problem has been resolved :)

gauravgtex007 avatar Jun 27 '17 10:06 gauravgtex007

Can't believe this is still a bug..

mvelebit avatar Jun 17 '18 16:06 mvelebit

I solved this issue by giving the Date field in Backend as Edm.DateTimeOffset (this will be a Timestamp field in ABAP) & converting the timestamp field to Date & Time by using function module - IB_CONVERT_FROM_TIMESTAMP.

maverick1812 avatar Jun 28 '18 07:06 maverick1812

if you are using moment, you can user the 'utc date' moment.utc('23-12-2011')

vishnucr avatar Jul 19 '18 15:07 vishnucr

This works for me

var DatePickerDate ='Thu Aug 09 2018 00:00:00 GMT+0100 (British Summer Time)';

var myDate = new Date(DatePickerDate).toISOString();

so myDate is now 2018-08-08T23:00:00.000Z

NDMCreative avatar Aug 02 '18 09:08 NDMCreative