1

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?

0

2 Answers 2

2

Looks like format you are after is ISO 8601 format:

$scope.event.Date = new Date().toISOString(); // "2015-03-04T14:28:19.616Z"
Sign up to request clarification or add additional context in comments.

3 Comments

hm, but if i select future date or something so? what i need to pass to formatter (on input is string with date like 2061-11-20)?
Not sure I get you. You want $scope.event.Date to be future date and also formatted to ISO 8601?
like this: if date is today (like 04-03-2015) then use current time too, but if future or past, just add there 0:00 as time to date also what i mean: for example your code get's current datetime, but how to get my input's date, and append there time?
0

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'),
  };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.