byCoder
byCoder

Reputation: 9184

javascript (angularJS): add current time to date and format

In my app user select date, but i need to send to the server current time too.

Here is my init code:

  $scope.event = {
    'Name': '',
    'Date': $filter('date')(new Date(), 'yyyy-MM-dd'),
  };

but i need to change it to such format:

2015-01-20T20:00:00Z

is it real? how could i append current time to my input's value in format as i posted here?

i do it like this:

      var x = new Date(); 
      var h = x.getHours(); 
      var m = x.getMinutes(); 
      var s = x.getSeconds(); 
      var z = x.getTimezoneOffset();
      $scope.event.Date = $scope.event.Date + 'T' + h + ':' + m + ':' + s + z;

but seems that this code is to ugly, maybe i do something wrong?

Upvotes: 1

Views: 2208

Answers (2)

davidpaquipalla
davidpaquipalla

Reputation: 183

Have a look the examples in official API:

https://docs.angularjs.org/api/ng/filter/date

You can see something like this:

{{1288323623006 | date:'yyyy-MM-dd HH:mm:ss Z'}}: 2010-10-29 04:40:23 +0100

Then you can use 'yyyy-MM-ddTHH:mm:ssZ' as format instead of 'yyyy-MM-dd'

$scope.event = {
    'Name': '',
    'Date': $filter('date')(new Date(), 'yyyy-MM-ddTHH:mm:ssZ'),
  };

Upvotes: 0

dfsq
dfsq

Reputation: 193261

Looks like format you are after is ISO 8601 format:

$scope.event.Date = new Date().toISOString(); // "2015-03-04T14:28:19.616Z"

Upvotes: 2

Related Questions