urish / ngx-moment

moment.js pipes for Angular
MIT License
1.17k stars 154 forks source link

Utc to local #160

Open rsrgithub opened 7 years ago

rsrgithub commented 7 years ago

Did you search for duplicate issue? Yes

I have utc date/time [2017-09-07T00:14:47]. I am in PST zone. The equivalent local date [PST] should be 2017-09-06 but pipe amLocal shows 2017-09-07.

below is the code - {{'2017-09-07T00:14:47' | amUtc | amLocal | amDateFormat: 'YYYY-MM-DD'}}

This time is returned by ASP.NET WebAPI.

Am I doing something incorrectly? Could you please guide me?

Plunker - http://plnkr.co/edit/sJlX7SpLwY7QPaDeqDUx?p=preview

Hesesses commented 7 years ago

122

bedrosenator commented 7 years ago

Here is the right code in file node_modules/angular2-moment/local.pipe.js var LocalTimePipe = (function () { function LocalTimePipe() { } LocalTimePipe.prototype.transform = function (value) { return moment.utc(value).local(); }; return LocalTimePipe; }());

So it must be return moment.utc(value).local(); in line 16

connormlewis commented 7 years ago

Try updating to the newest version of this package and give {{'2017-09-07T00:14:47' | amFromUtc | amLocal | amDateFormat: 'YYYY-MM-DD'}} a try

rsrgithub commented 7 years ago

I tried {{'2017-09-07T00:14:47' | amFromUtc | amLocal | amDateFormat: 'YYYY-MM-DD'}} after taking latest version. It still returns 2017-09-07.

Although if I try {{'2017-09-07T00:14:47Z' | amFromUtc | amLocal | amDateFormat: 'YYYY-MM-DD'}} [NOTICE 'Z' in datetime] It returns expected value as 2017-09-06.

I am not sure if 'Z' must be appended at the end to UTC datetime or this library API can be updated to convert by appending 'Z' automatically.

ASP.NET WEB API does not return time appended with 'Z'

connormlewis commented 7 years ago

I tried using native moment and it seemed to work without the 'Z' (moment.utc('2017-09-07T00:14:47').local().format('YYYY-MM-DD') is what the pipes should translate to). What timezone are you trying this in? What happens if you just do {{ '2017-09-07T00:14:47' | amFromUtc | amDateFormat }}? I believe when it prints out the formatted date string it should include the 'Z' in the time.

rsrgithub commented 7 years ago

As I mentioned in my initial post, I am in PST time zone. '2017-09-07T00:14:47' is in UTC. On converting it to PST timezone, the date should be 2017-09-06 [ http://coderstoolbox.net/unixtimestamp/ ].

@ What happens if you just do {{ '2017-09-07T00:14:47' | amFromUtc | amDateFormat }}? It outputs - 2017-09-07T00:14:47Z. You are correct it includes 'Z' at the end.

Could you please point out what am I doing incorrectly?

connormlewis commented 7 years ago

I just tried from a blank angular project generated with the CLI and it gave the expected results in the Eastern timezone. What does {{ '2017-09-07T00:14:47' | amFromUtc | amLocal | amDateFormat }} output?

rsrgithub commented 7 years ago

@ What does {{ '2017-09-07T00:14:47' | amFromUtc | amLocal | amDateFormat }} output? It outputs - 2017-09-07T00:14:47Z.

What was the output when you tried on a blank angular project for '2017-09-07T00:14:47' in Eastern Time Zone? The expected result should be 2017-09-06 as EST is 4 hrs behind to UTC.

connormlewis commented 7 years ago

That is the output that I got when I tried it in Eastern timezone. Could you try in a blank project? It seems as though you are doing everything correct