1

I am using InputMask js (version 4.x) for date/datetime formatting.

I am trying to format a backend date which comes like this: 2018-03-23T00:00:00.000+01:00 and want to display it like this: yyyy.mm.dd

I tried to set the inputFormat attribute to 'yyyy.mm.dd' but it's not working. Instead, seems to work with mm.dd.yyyy, but I would like to display that backend date in different formats.

Here is the code:

(function($){


  $('input').val(new Date('2018-03-23T00:00:00.000+01:00').toLocaleDateString());

  $('input').inputmask("datetime",{
    inputFormat: 'yyyy.mm.dd'
  });


})(jQuery)

And a JSFiddle: https://jsfiddle.net/zq46eg90/8/

I would expect here to have '2018.03.23' displayed, but the actual result is something else (3232.01.08)

Evelyne
  • 33
  • 6
  • did you try my answer? is there any issue? – Hien Nguyen Apr 11 '19 at 15:22
  • Yes, tried it. Seems to work but I don't want right now to add another library to the project. I will go for now with this solution: $('input').val(new Date('2018-03-23T00:00:00.000+01:00').toLocaleDateString('lt-LT')); – Evelyne Apr 12 '19 at 09:00

2 Answers2

0

You can use momentjs for your case.

(function($){

  var date = moment(new Date('2018-03-23T00:00:00.000+01:00').toLocaleDateString());
  $('input').val(date.format('YYYY.MM.DD'));

  $('input').inputmask("datetime",{
    inputFormat: 'yyyy.mm.dd'
  });


})(jQuery)

See demo https://jsfiddle.net/viethien/qLgsob41/3/

Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
0

Try using lt-LT locale. (more locales)

$('input').val(new Date('2018-03-23T00:00:00.000+01:00').toLocaleDateString('lt-LT'));
User863
  • 19,346
  • 2
  • 17
  • 41