Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a date type input to a timestamp [closed]

I need to convert the date that I am getting from an HTML5 date input to a timestamp and save it as a variable.

HTML:

<form>
<section class="input">
  <input type="date" id="date" name="maintenanace_date"/>  
</section>
<section class="input">
   <input type="time" id="time" name="maintenance_time" placeholder="EST"/>
</section>
</form>

like image 658
YarinDekel Avatar asked Oct 19 '25 03:10

YarinDekel


1 Answers

var date = new Date(document.getElementById("time").value);
var timestamp = date.getTime();

If you need to use it in a serverside language like PHP, you also need to divide it by 1000.

However, this depends on the format the date is entered in. If you're using a non-default date format, you'll need to split it up and convert it into year, month and date before instantiating the Date object.

like image 75
vdwijngaert Avatar answered Oct 21 '25 18:10

vdwijngaert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!