0

I have a date value from backend formatted date1 =

{
  "date": "2021-11-05 14:07:25.000000",
  "timezone_type": 3,
  "timezone": "Europe/Istanbul"
}

I want to calculate time from now with new Date() like new Date() - date1 but .getTime() or any other methods does not work with date object "date1". I tried to get date1.date but it is not allowed. So I could not find a way to solve that.

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
osman
  • 33
  • 7
  • Does this answer your question? [How to initialize a JavaScript Date to a particular time zone](https://stackoverflow.com/questions/15141762/how-to-initialize-a-javascript-date-to-a-particular-time-zone) – Heretic Monkey Nov 05 '21 at 13:44

1 Answers1

-1

Here is the solution

new Date().getTime() - new Date(JSON.parse(JSON.stringify(date1)).date).getTime();
Roy
  • 7,811
  • 4
  • 24
  • 47
osman
  • 33
  • 7
  • What is this the solution to? This will get a number of milliseconds. The OP wants a `Date` object. Why are you stringifying and parsing `date1`? Please take a moment and read [ask] for tips on what Stack Overflow expects from answers. – Heretic Monkey Nov 05 '21 at 13:46
  • I wanted to calculate time. Not date object. Please take a moment and read what is asked before you comment the answer. I am stringifying and parsing date1 because I could not find another way to calculate time new Date() - date1 as asked. – osman Nov 05 '21 at 13:57
  • Please take a moment and read the link added to your question and realize that there is no "time" type in JavaScript. There is only a `Date` type. You can *format* a `Date` object to show only the time portion. There are many questions about that, https://stackoverflow.com/q/18229022/215552, for instance. – Heretic Monkey Nov 05 '21 at 14:04
  • No, I will not. – osman Nov 05 '21 at 14:09
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 05 '21 at 15:14