0

I want to fill a select (dropdownlist) with hours from 00 to 23 using angular in the format "13:00","08:00" , i tied the following code :

<option ng-repeat="n in [].constructor(24) track by $index" value="{{$index}}">{{$index | date:'HH:mm'}}</option>

but the result is "02:00" for all 24 item of the select

3 Answers 3

2

Since $index is just a series of integers (0, 1, 2, 3 ...) and the filter treats is as a timestamp, what you're increasing here is just milliseconds.

For the hour to change you'd have to increase the number by 3 600 000 milliseconds, so this will do it:

<option ng-repeat="n in [].constructor(24) track by $index" value="{{$index}}">
    {{$index * 3600000 | date:'HH:mm'}}
</option>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks @TomekSułkowski ,it works but it starts with 02:00 for value 0, 03:00 for value 1, 04:00 for value 4 , ...
1

Using the same calculation in Tomek Sulkowski's answer but use UTC in the date format so the value is independent to the local time zone.

<option ng-repeat="n in [].constructor(24) track by $index" value="{{$index}}">
    {{$index * 3600000 | date:'HH:mm' : 'UTC'}}
</option>

1 Comment

how to select a specific item using model ?? <select id="time_hours" ng-model="v.Time"> <option ng-repeat="n in [].constructor(24) track by $index" value="{{$index}}">{{$index>9?$index:"0"+$index}}:00</option> </select>
0

Another answer - but simpler : <option ng-repeat="n in [].constructor(24) track by $index" value="{{$index}}">{{$index>9?$index:"0"+$index}}:00</option>

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.