I have an HTML form, whereby a user fills in details and enters the date and time(the date and time should be ahead of the current time). I want to calculate the time difference using local time. Am able to get data from the form on click the submit button (javascript). I need an idea of how to calculate time based on local timezones.
Asked
Active
Viewed 179 times
0
-
What have you tried so far? – Daweed Apr 16 '21 at 09:38
-
JS works in local time by default, and I imagine finding the difference between two dates has been covered dozens of times in other questions. E.g. [Get difference between 2 dates in JavaScript?](https://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript) – DBS Apr 16 '21 at 09:39
2 Answers
0
By using the Date
function in JavaScript you can get both dates as a Unix Timestamp, to easily compare them. I would guess you have and <input type="datetime-local">
that allows for easily choosing a time. With that input type you can convert it to Unix easily:
const date = new Date(dateInput.value);
console.log(date - Date.now())
Where dateInput
is your input element

J0R1AN
- 583
- 7
- 10
0
Javascript by default will create a new Date()
based on the users OS.
Not sure what you plan on doing with the dates however it may be worth converting those dates to UTC and then calculate the difference using something like date-fns

Jack
- 99
- 1
- 10
-
in my case, I want to use the time difference to return a certain condition. like for example if I take a case like a client wants to hire a writer and the client inputs the time of submission of the task. then i want to declare a fixed charge if the task has like 8 hours before being submitted , and another price which should be included on the fixed charge incase the time of submission is less than 8 hours . – vinny MUSYOKA Apr 16 '21 at 16:41