angular-datepicker icon indicating copy to clipboard operation
angular-datepicker copied to clipboard

Date time with timezone

Open taguenizy opened this issue 9 years ago • 4 comments

Hello,

When using the dateTime directive with a defined timezone it doesn't with the correct timezone parsing as it happens with the datePicker directive.

For instance, <div date-picker="date" timezone="Asia/Hong_Kong" view="hours"></div> <input date-time ng-model="date" timezone="Asia/Hong_Kong" />

When you update it gets correct so I'm assuming it's something related with the initialization of the directive.

As a quick hack I've added value = (value && timezone) ? datePickerUtils.createMoment(value) : value; in the formatter function of the dateTime directive. It solves my particular problem but was hoping for a more permanent and elegant solution.

Best regards

taguenizy avatar Jul 18 '16 12:07 taguenizy

I'm either unable to replicate the problem, or I am not understanding what the problem is.

See here for a plunkr with my attempt to reproduce your issue. The timezones appear correct to me both on initialization and after selecting new date values.

DanTalash avatar Feb 24 '17 16:02 DanTalash

I might have left out an important detail. As the date I'm providing is a string.

See the plunkr. Based on your example I just passed the today date to a string and as you can see datePicker and dateTime behave differently. DatePicker still shows what we intended but DateTime doesn't.

taguenizy avatar Feb 24 '17 16:02 taguenizy

The problem was with the mFormat filter, which completely ignores timezones when passed a non-moment object.

Here is a plunkr with the filter fixed. I will update my current PR with this fix tomorrow.

DanTalash avatar Feb 25 '17 05:02 DanTalash

Here is a workaround solution, just re-define 'mFormat' filter in your app:

  app.filter('mFormat', function () {
    return function (m, format, timezone) {  
      m = moment.isMoment(m) ? m : moment(m);  
      if (m.isValid()) {  
        return timezone ? m.tz(timezone).format(format) : m.format(format);  
      }  
    };  
  });

ovaldi avatar May 09 '17 06:05 ovaldi