-1

I'd like to get the Epoch timestamp of a date being entered by a user, but convert the time to 12:01 am in JavaScript.

How do I do that?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
S.Troy
  • 117
  • 2
  • 8
  • `dateobject.setHours(0,1,0,0)` – Bravo Aug 18 '21 at 12:22
  • This doesn't make any sense. Epoch timestamps count the number of milliseconds since a specific date and time (1970-01-01T00:00:00.000Z for JavaScript Dates). How do you "convert" that to a specific time, other than the one that corresponds to the number of milliseconds since the epoch? – Heretic Monkey Aug 18 '21 at 12:26
  • Also, please show what research you've done, and any attempts you've made to solve the problem yourself. – Heretic Monkey Aug 18 '21 at 12:27
  • can you show what you've got so far, so we know what you want converted – Bravo Aug 18 '21 at 12:27
  • Basically a duplicate of [how to convert a string to a Unix timestamp in javascript?](https://stackoverflow.com/q/18634087/215552) and [Javascript: Set a new Date to tomorrow 8am](https://stackoverflow.com/q/36158193/215552) – Heretic Monkey Aug 18 '21 at 12:48

1 Answers1

1

Take the timestamp and pass it while creating a instance of the Date class like:

let timestamp = 1629289414;
let dateInstance = new Date(timestamp * 1000);

console.log(dateInstance); // Wed Aug 18 2021 14:23:34....

From there on you have many ways to work with dateInstance. To get the 12:00am result from this it's more a string manipulation/adjusting thing.

Just check out the documentation on the the javascript Date instance: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Date

If you have further questions just post the code you've written and maybe I can help you out

db3000
  • 400
  • 5
  • 16
  • *string manipulation* ... you mean date manipulation ... `dateobject.setHours(0,1,0,0)` ... – Bravo Aug 18 '21 at 12:45
  • with string manipulation I ment to manipulate the time part of a date (string) to meet the requirements from the question with 12:01 am. So you have a date string containing "14:50" and get as result "2:50pm" and so on. I don't think/know if you can do that with setHours() function. – db3000 Aug 18 '21 at 12:54
  • you do it exactly like I said ... then you console.log it and the time will be 00:01:00.000 - or 12:01 AM – Bravo Aug 18 '21 at 12:56