0

Is there a way to elegantly convert the value of this particular form field (input type="date") to a UNIX timestamp?

console.log($scope.jobDueDate) gives me "Wed Jun 08 2016 00:00:00 GMT+0530 (IST)" and I really don't want to define a dictionary mapping months to numbers.

Prajeeth Emanuel
  • 647
  • 1
  • 12
  • 24
  • 1
    That's just the string representation of a `Date` object. You can simply use `$scope.jobDueDate.valueOf()` ~ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/valueOf – Phil Jun 21 '16 at 10:05

2 Answers2

2

You can do it as described in this SO answer

$scope.jobDueDate.getTime()

EDIT:

getTime would give you milliseconds, which you then need to divide by 1000

$scope.jobDueDate.getTime()/1000
Community
  • 1
  • 1
Michele Ricciardi
  • 2,107
  • 14
  • 17
1

Just use

var unixtime = Date.parse($scope.jobDueDate)/1000;

Explanation: Date.parse($scope.jobDueDate) Gives time in milliseconds since 1970. So you divide by a 1000 to get time in seconds.

Njuguna Mureithi
  • 3,506
  • 1
  • 21
  • 41