Reputation: 1088
I have a date in local format , I changed it to UTC format . After UTC i want to extract date and time in UTC . But whenever I am trying to extract time or date for that matter , it is considering local time only , why is that ?
this.utcDate = this.datePipe.transform(this.onceDate,'medium');
console.log('+=timezonenene=+', this.utcDate);
this.utcDateStr = new Date(this.utcDate).toUTCString();
console.log('++ date is ++', this.utcDateStr);
this.utcDateStr = new Date(this.utcDateStr).toISOString();
console.log('++ date is ++', this.utcDateStr);
this.showDate = this.datePipe.transform(this.utcDateStr, 'yyyy-MM-dd');
this.showTime = this.datePipe.transform(this.utcDateStr, 'HH:mm');
console.log('+=time date zonenene date str =+', this.showDate);
console.log('+=timezonenene date str=+', this.showTime);
output :
+=timezonenene=+ Apr 7, 2020, 5:41:16 PM
++ date is ++ Tue, 07 Apr 2020 12:11:16 GMT
++ date is ++ 2020-04-07T12:11:16.000Z
+=time date zonenene date str =+ 2020-04-07
+=timezonenene date str=+ 17:41
I want last logged time as : 12:11 not 17:41
Upvotes: 0
Views: 32
Reputation: 241475
Your question appears to be about the Angular DatePipe (though you're calling the transform
function directly).
From the docs about the timezone
parameter:
When not supplied, uses the end-user's local system timezone.
Thus, to get UTC output, you can change your code to provide the time zone parameter 'UTC'
after the format parameter, on each call to datePipe.transform
.
Also, I'd reconsider why you are producing strings just to parse them again. Usually this is unnecessary, and can lead to errors. if this.onceDate
is a Date
object, you should be able to use that throughout your code, rather than creating this.utcDateStr
just to parse and transform it again. (Though it's difficult to tell from your code if that is the case. In the future, please supply a minimal, reproducible example.)
Upvotes: 1