Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
Javascript - Setting Custom Time in Unix Timestamp

I'm working on a program that takes input from a user for a time. The program will take the information and automatically generate a Unix Timestamp using the current date as the date in the timestamp.

For example:

Daniel wants to generate a Unix Timestamp for 8:30 AM on Christmas Day. He runs a command /unix 8:30, and the console prints out 1640421000.

What's the best way to achieve this? I understand how to generate a Unix Timestamp, but how do I edit just the time to the user input. Any help would be greatly appreciated.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can create a Date for the current date, set the time as required, then generate a time value in seconds.

If the user will always enter H:mm and you don't need to validate the input, then the following will do:

let time = '8:30';
let tv = new Date().setHours(...(time.split(/\D/)), 0) / 1000 | 0;

// Check value
console.log(new Date(tv * 1000).toString());

However, the input should be validated (e.g. hours 0-23, minutes 0-59).

about 4 years ago · Juan Pablo Isaza Report

0

I just went with the following:

const time = interaction.options.getString('time');
const date = new Date().toISOString().slice(0, 10);

console.log(Math.round(new Date(`${date} ${time}:00`).getTime()/1000));

Seems to work for me.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!