1

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 combining 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
Salman A
  • 262,204
  • 82
  • 430
  • 521
Furqan Asghar
  • 3,670
  • 4
  • 23
  • 27
  • what do you need the timestamp for and in what format? – Foreever Jan 22 '14 at 05:04
  • possible duplicate of [How do you get a timestamp in JavaScript?](http://stackoverflow.com/questions/221294/how-do-you-get-a-timestamp-in-javascript) – Foreever Jan 22 '14 at 05:10

2 Answers2

4

I'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" */
Mr. Lavalamp
  • 1,860
  • 4
  • 17
  • 29
  • If you have any trouble following the numbers in that calculation I would be happy to switch it to variables and/or show you how to put it in a function. – Mr. Lavalamp Jan 22 '14 at 05:33
2

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()

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531