i have an long const values below like this ,is it possible to create in function in typescript to reduce a long const.
export const DepartTime =
['00.00', '00.30', '01.00', '01.30', '02.00', '02.30', '03.00', '03.30', '04.00', '04.30', '05.00', '05.30', '06.00', '06.30', '07.00', '07.30', '08.00', '08.30', '09.00', '09.30', '10.00', '10.30', '11.00', '11.30', '12.00', '12.30', '13.00',
'13.30', '14.00', '14.30', '15.00', '15.30', '16.00', '16.30', '17.00', '17.30', '18.00', '18.30', '19.00', '19.30',
'20.00', '20.30', '21.00', '21.30', '22.00', '22.30', '23.00', '23.30'
];
These values need to be bind in angular dropdown.
export class SelectOverviewExample implements OnInit {
days=[];
times =[];
ngOnInit(){
[...this.days] = weekdays;
[...this.times]=DepartTime;
}
in html.
<mat-form-field>
<mat-select placeholder="select Weekdays">
<mat-option *ngFor="let time of times" [value]="time">
{{time}}
</mat-option>
</mat-select>
</mat-form-field>
or any third party framework will support
I have tried this and getting below result:
var toInt = time => ((h,m) => h*2 + m/30)(...time.split(':').map(parseFloat)),
toTime = int => [Math.floor(int/2), int%2 ? '30' : '00'].join(':'),
range = (from, to) => Array(to-from+1).fill().map((_,i) => from + i),
eachHalfHour = (t1, t2) => range(...[t1, t2].map(toInt)).map(toTime);
console.log(eachHalfHour('00:00', '23:30'))
But i need single digit have start with 00:00,00:30,01.00,01.30 ... 09.30. O/p
by using moment js , i have get a solution but need 0 prefix for single digits
const locale = 'en'; // or whatever you want...
const hours = [];
moment.locale(locale); // optional - can remove if you are only dealing with one locale
for(let hour = 0; hour < 24; hour++) {
hours.push(moment({ hour }).format('H:mm'));
hours.push(
moment({
hour,
minute: 30
}).format('H:mm')
);
}
console.log(hours);
i have an long const values below like this ,is it possible to create in function in typescript to reduce a long const.
export const DepartTime =
['00.00', '00.30', '01.00', '01.30', '02.00', '02.30', '03.00', '03.30', '04.00', '04.30', '05.00', '05.30', '06.00', '06.30', '07.00', '07.30', '08.00', '08.30', '09.00', '09.30', '10.00', '10.30', '11.00', '11.30', '12.00', '12.30', '13.00',
'13.30', '14.00', '14.30', '15.00', '15.30', '16.00', '16.30', '17.00', '17.30', '18.00', '18.30', '19.00', '19.30',
'20.00', '20.30', '21.00', '21.30', '22.00', '22.30', '23.00', '23.30'
];
These values need to be bind in angular dropdown.
export class SelectOverviewExample implements OnInit {
days=[];
times =[];
ngOnInit(){
[...this.days] = weekdays;
[...this.times]=DepartTime;
}
in html.
<mat-form-field>
<mat-select placeholder="select Weekdays">
<mat-option *ngFor="let time of times" [value]="time">
{{time}}
</mat-option>
</mat-select>
</mat-form-field>
or any third party framework will support
I have tried this and getting below result:
var toInt = time => ((h,m) => h*2 + m/30)(...time.split(':').map(parseFloat)),
toTime = int => [Math.floor(int/2), int%2 ? '30' : '00'].join(':'),
range = (from, to) => Array(to-from+1).fill().map((_,i) => from + i),
eachHalfHour = (t1, t2) => range(...[t1, t2].map(toInt)).map(toTime);
console.log(eachHalfHour('00:00', '23:30'))
But i need single digit have start with 00:00,00:30,01.00,01.30 ... 09.30. O/p
by using moment js , i have get a solution but need 0 prefix for single digits
const locale = 'en'; // or whatever you want...
const hours = [];
moment.locale(locale); // optional - can remove if you are only dealing with one locale
for(let hour = 0; hour < 24; hour++) {
hours.push(moment({ hour }).format('H:mm'));
hours.push(
moment({
hour,
minute: 30
}).format('H:mm')
);
}
console.log(hours);
Share
Improve this question
edited Sep 6, 2018 at 20:01
Mohamed Sahir
asked Sep 6, 2018 at 15:31
Mohamed SahirMohamed Sahir
2,5439 gold badges49 silver badges78 bronze badges
3
- Sure, you can put a loop in a function and generate that array. Have you tried anything? – Bergi Commented Sep 6, 2018 at 16:02
- I have tried above code but it starts with "0.00","0.30" ,...9:30., but i need "00.30 to 09.30" and remaining things be same.if you find better solution ,more helpful – Mohamed Sahir Commented Sep 6, 2018 at 17:00
- That looks good already! For the remaining problems see stackoverflow./q/8043026/1048572 – Bergi Commented Sep 6, 2018 at 17:27
7 Answers
Reset to default 2Short answer: change your moment.format to .format('HH:mm'));
You can ensure you get the double digits by just adding an extra 'H' to your existing moment config. ie go from this:
const locale = 'en'; // or whatever you want...
const hours = [];
moment.locale(locale); // optional - can remove if you are only dealing with one locale
for(let hour = 0; hour < 24; hour++) {
hours.push(moment({ hour }).format('H:mm'));
hours.push(
moment({
hour,
minute: 30
}).format('H:mm')
);
}
console.log(hours);
to this
const locale = 'en'; // or whatever you want...
const hours = [];
moment.locale(locale); // optional - can remove if you are only dealing with one locale
for(let hour = 0; hour < 24; hour++) {
hours.push(moment({ hour }).format('HH:mm'));
hours.push(
moment({
hour,
minute: 30
}).format('HH:mm')
);
}
console.log(hours);
Hope that helps! :)
Below Solution work for me:
const hours = [];
Array.apply(null, { length: 24 }).map((value, hour) => {
hours.push(
moment({ hour }).format('HH:mm'),
moment({ hour, minute: 15 }).format('HH:mm'),
moment({ hour, minute: 30 }).format('HH:mm'),
moment({ hour, minute: 45 }).format('HH:mm')
);
});
console.log(hours);
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.29.4/moment.min.js"></script>
Based on your requirements you can push those values to the hours array.
Here is a function that will return the key-value pair based on the given interval(minutes: 60, 30, 15, 10, 5), where the key is in 24-hour format and the display value is in 12-hour format.
// Interval values can be(minutes): 60 | 30 | 15 | 10 | 5
function getTimeDDL(intervalMinutes) {
let formatHour = (hour, minutes) => {
let minutesPadded = minutes.toString().padStart(2, "0");
return {
value: (hour < 10 ? "0" + hour : hour) + `:${minutesPadded}`,
viewValue: hour < 12 || hour == 24 ? (hour < 24 ? hour : 12) + `:${minutesPadded} AM` : (hour - 12 || 12) + `:${minutesPadded} PM`,
};
};
let timesDDL = [];
let hour = 1;
while (hour <= 24) {
timesDDL.push(formatHour(hour, 0));
if (hour < 24) {
let steps = 60 / intervalMinutes;
let i = 1;
while (i < steps) {
let minutes = intervalMinutes * i;
timesDDL.push(formatHour(hour, minutes));
i++;
}
}
hour++;
}
return timesDDL;
}
// Interval values can be(minutes): 60 | 30 | 15 | 10 | 5
console.log(getTimeDDL(30));
/** Output
[
{
"value": "01:00",
"viewValue": "1:00 AM"
},
{
"value": "01:30",
"viewValue": "1:30 AM"
},
..
..
{
"value": "24:00",
"viewValue": "12:00 AM"
}
]
**/
I don't think you can reduce it. This may not help, but here is JS version :
var foo = [];
for (var i = 0; i <= 48; i++) {
var n = i%2==0 ? i/2+'.00' : (i+1)/2-1+'.30';
if(n<10) //zero-left padding
n = '0'+n;
foo.push(n);
}
console.log(foo);
Below code works for me I have tried using https://stackoverflow./a/8043061/9516784
var hrs = [];
for (var i = 0; i <= 48; i++) {
var n = i%2==0 ? i/2+'.00' : (i+1)/2-1+'.30';
if(n<10) {
n = '0'+n;
}
hrs.push(n);
}
You can just use division by 2.
let DepartTime = [...Array(48)].map((e, i) => {
return (i/2<10 ? '0' : '') + (i/2 - i/2 % 1) + (i/2 % 1 != 0 ? '.30' : '.00');
});
console.log(DepartTime);
Passing a string in hh:mm
format to your function is making it more plicated than necessary when you could just pass numbers to it.
You can use padStart to ensure a number converted to a string has leading zeroes.
As for the loop, this seems like the perfect situation to use a generator.
function* generateTimes(startHour, stopHour) {
for (let h = startHour; h < stopHour; ++h) {
const hh = h.toString().padStart(2, '0');
yield `${hh}.00`;
yield `${hh}.30`;
}
}
console.log([...generateTimes(0, 24)]);