-4

in C# i can use DateTime.UtcNow.AddMinutes((-1) * 300); but how can i achieve the same result in Typescript? I am using var dummydate: any = new Date().toUTCString(); to convert it in UTC. Again if i change the dummydate from string to date. This does not preserver the UTC TimeZone. After conversion you get the date in your local timezone. Hence Setminutes is applied on local time zone not on UTC.

Himani

Vanu
  • 63
  • 1
  • 12
  • I am using setMinutes. Issue is I need to apply this to UTC date. var dummydate: any = new Date().toUTCString(); but when i convert the dummydate string to date it converts it to local time and hence on adding minutes it provides me wrong time. – Vanu Oct 07 '19 at 09:32

1 Answers1

0

For date manipulation in Javascript / Typescript u can use momentjs:

MomentJS

MomentJS Manipulation

U can then do something like:

moment().add(7, 'days');

More info in this post:

How to add 30 minutes to a JavaScript Date object?

  • I am using setMinutes to add minutes. Issue is when i convert the date to UTC using toUTCstring() which returns string and when i convert the string back to date . It does not preserve time zone and give me localtime zone. – Vanu Oct 07 '19 at 09:36
  • 1
    @HimaniVerma I am not really sure what you want to achieve. Instead of converting it to string and back to date. You can reuse the date object, and just ask the string when it is needed. That way you always have the original date. – Mark van Turnhout Oct 07 '19 at 12:41
  • Sorry if i sound confusing. Actually i want to add minutes to UTC date . User will select a time zone as per his choice and when i convert the current date to UTC format using toUTCString() it returns date as string and when i try to convert the same string to date , it returns me my system time zone not the UTC one. Issue is when i convert UTC date string to date it gets converted to local time zone. – Vanu Oct 09 '19 at 05:22