1

In my application, I am using my application to retrieve the date information from the server. e.g:1533675600000. How do I get today's raw date number in this way? Every day, I want to refresh this daily raw date information and show only the information that matches the date of that day on my screen. So I ask you, how do I convert the date information I received with the new Date () function to the raw form of numbers in this way?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
fatih
  • 67
  • 1
  • 12
  • cast it back to Date object with `new Date(1533675600000)` – Aleksey Solovey Aug 14 '18 at 14:53
  • @AlekseySolovey The new date that I created should be just numbers or I should be able to transform it. That's wrong, that can't be the solution. – fatih Aug 14 '18 at 15:00
  • @Fatih, you can use new Date(myDate).getTime() to get 'numeric' date. Use a date pipe in your html to format it e.g. {{myDate | date:'dd-MMM-yyyy;}} – Farasi78 Aug 14 '18 at 15:01
  • @Farasi78 Unfortunately. I have not been able to explain the situation. I have a date information and format is: 1533675600000. I want to compare it to the current date with this format but when I try to do it with newDate () this is invalid because the newDate format is not the same as my date format from the server. – fatih Aug 14 '18 at 15:08
  • @Fatih cast both of them with Date object then: `new Date(1533675600000).getTime() == new Date(current_date).getTime()` – Aleksey Solovey Aug 14 '18 at 15:49

1 Answers1

2

I'm assuming by raw date you mean Epoch time.

From Wikipedia:

Epoch time is a system for describing a point in time, defined as an approximation of the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970.

Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.

To get current Epoch time, simply convert the value returned from Date.now() into seconds like

Math.floor(Date.now() / 1000); // current time in seconds

Then perform any comparisons you want with this value.

Nikhil
  • 6,493
  • 10
  • 31
  • 68