i have a datepicker and some time slots in my view. the time slots are checkboxes and the value of checkbox should be a timestamp which i aim to get by bining the value from datepicker and the checkbox slot that was checked by the user. Is there any way to do this using javascript of jquery.
Example:
$('#inline_datepicker').datepicker("getDate") + "02:30PM" => create timestamp
i have a datepicker and some time slots in my view. the time slots are checkboxes and the value of checkbox should be a timestamp which i aim to get by bining the value from datepicker and the checkbox slot that was checked by the user. Is there any way to do this using javascript of jquery.
Example:
$('#inline_datepicker').datepicker("getDate") + "02:30PM" => create timestamp
Share
Improve this question
edited Jan 22, 2014 at 6:55
Salman Arshad
273k84 gold badges444 silver badges534 bronze badges
asked Jan 22, 2014 at 4:55
Furqan AsgharFurqan Asghar
3,8704 gold badges25 silver badges27 bronze badges
2
- what do you need the timestamp for and in what format? – Foreever Commented Jan 22, 2014 at 5:04
- possible duplicate of How do you get a timestamp in JavaScript? – Foreever Commented Jan 22, 2014 at 5:10
2 Answers
Reset to default 4I'm not clear on exactly what you're looking for here, but to just get a basic timestamp in string format using milliseconds (from Jan. 1, 1970 at midnight, as is the standard) you can just do this using vanilla javascript:
var timestamp = new Date(2014,1,3,18).getTime();
The format is yyyy/mm/dd/hh and it is zero based and the hours use a 24 hour clock, also called military time if you are from the United States. The above example gives the timestamp for February 4, 2014 at 6pm.
You can then always add time to the time stamp by adding time to that value, too. 1 day is 24 hours, one hour is 60 minutes, one minute is 60 seconds, and one second is 1000 milliseconds. That means after creating a timestamp, if you wanted to add 4 days, 13 hours, 49 minutes, and 6 seconds, you could do this:
var timestamp = new Date(2014,1,3,18).getTime();
timestamp += (((4*24+13)*60+49)*60+6)*1000;
Finally, you can always convert back to a human-readable timestamp using the .toUTCString() method in conjunction with creating a new Date object. The final code would read as such:
var timestamp= new Date(2014,1,3,18).getTime();
console.log(new Date(timestamp).toUTCString());
/* Outputs "Sun, 04 Feb 2014 18:00:00 GMT" */
timestamp += (((4*24+13)*60+49)*60+6)*1000;
console.log(new Date(timestamp).toUTCString());
/* Outputs "Sun, 09 Feb 2014 07:49:06 GMT" */
Try
var date = $('#inline_datepicker').datepicker("getDate");
//24 hour clock
date.setHours(14, 30);
console.log(date)
Demo: Fiddle
To get the time in milliseconds use date.getTime()