Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting new Date() to UTC format in typescript, angular

I have a request from angular, which sends date to server.

const activationDate = new Date();

I need to convert this to UTC date format before sending the request

In my model, I have a Date variable to pass the date to server

export class PersonDetails {
  id: string;
  activationDate?: Date;
}
like image 908
RN92 Avatar asked Jul 09 '26 02:07

RN92


1 Answers

personModel: PersonDetail;    

const activationDate = new Date();

this.personModel.activationDate = new Date(activationDate.getUTCFullYear(),
                                            activationDate.getUTCMonth(),
                                            activationDate.getUTCDate(),
                                            activationDate.getUTCHours(),
                                            activationDate.getUTCMinutes(),
                                            activationDate.getUTCSeconds()
                                            );

OR Can use a separate method to get UTCDate

const activationDate = this.getNowUTC();    

private getNowUTC() {
  const now = new Date();
  return new Date(now.getTime() + (now.getTimezoneOffset() * 60000));
}

A good article on converting DateTime

like image 138
RN92 Avatar answered Jul 11 '26 19:07

RN92



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!