convert 24 hours into 12 hours
i have ionic code that load times for me but it make it 24 hours and i want it in 12 hours with AM or PM
i tried many things but not work
this is my code
loadTimes() {
this.availabilityTimes = new Array<Array<string>>();
for(let i = 0; i < 7; i++){
this.availabilityTimes.push(new Array<string>());
}
let date = new Date();
for (let avail of this.availabilities) {
let dateStart = new Date(date.getFullYear(), date.getMonth(), date.getDate(), Number(avail.start.split(':')[0]), Number(avail.start.split(':')[1]), 0);
let dateEnd = new Date(date.getFullYear(), date.getMonth(), date.getDate(), Number(avail.end.split(':')[0]), Number(avail.end.split(':')[1]), 0);
let availabilityTime = new Array<string>();
let time: number = dateStart.getTime();
while(time <= dateEnd.getTime()) {
let dateIn = new Date(time);
availabilityTime.push((dateIn.getHours()<10?('0'+dateIn.getHours()):String(dateIn.getHours())) +':'+ (dateIn.getMinutes()<10?('0'+dateIn.getMinutes()):String(dateIn.getMinutes())) +':00');
time = time + 3600000;
}
javascript typescript ionic-framework ionic3
add a comment |
i have ionic code that load times for me but it make it 24 hours and i want it in 12 hours with AM or PM
i tried many things but not work
this is my code
loadTimes() {
this.availabilityTimes = new Array<Array<string>>();
for(let i = 0; i < 7; i++){
this.availabilityTimes.push(new Array<string>());
}
let date = new Date();
for (let avail of this.availabilities) {
let dateStart = new Date(date.getFullYear(), date.getMonth(), date.getDate(), Number(avail.start.split(':')[0]), Number(avail.start.split(':')[1]), 0);
let dateEnd = new Date(date.getFullYear(), date.getMonth(), date.getDate(), Number(avail.end.split(':')[0]), Number(avail.end.split(':')[1]), 0);
let availabilityTime = new Array<string>();
let time: number = dateStart.getTime();
while(time <= dateEnd.getTime()) {
let dateIn = new Date(time);
availabilityTime.push((dateIn.getHours()<10?('0'+dateIn.getHours()):String(dateIn.getHours())) +':'+ (dateIn.getMinutes()<10?('0'+dateIn.getMinutes()):String(dateIn.getMinutes())) +':00');
time = time + 3600000;
}
javascript typescript ionic-framework ionic3
add a comment |
i have ionic code that load times for me but it make it 24 hours and i want it in 12 hours with AM or PM
i tried many things but not work
this is my code
loadTimes() {
this.availabilityTimes = new Array<Array<string>>();
for(let i = 0; i < 7; i++){
this.availabilityTimes.push(new Array<string>());
}
let date = new Date();
for (let avail of this.availabilities) {
let dateStart = new Date(date.getFullYear(), date.getMonth(), date.getDate(), Number(avail.start.split(':')[0]), Number(avail.start.split(':')[1]), 0);
let dateEnd = new Date(date.getFullYear(), date.getMonth(), date.getDate(), Number(avail.end.split(':')[0]), Number(avail.end.split(':')[1]), 0);
let availabilityTime = new Array<string>();
let time: number = dateStart.getTime();
while(time <= dateEnd.getTime()) {
let dateIn = new Date(time);
availabilityTime.push((dateIn.getHours()<10?('0'+dateIn.getHours()):String(dateIn.getHours())) +':'+ (dateIn.getMinutes()<10?('0'+dateIn.getMinutes()):String(dateIn.getMinutes())) +':00');
time = time + 3600000;
}
javascript typescript ionic-framework ionic3
i have ionic code that load times for me but it make it 24 hours and i want it in 12 hours with AM or PM
i tried many things but not work
this is my code
loadTimes() {
this.availabilityTimes = new Array<Array<string>>();
for(let i = 0; i < 7; i++){
this.availabilityTimes.push(new Array<string>());
}
let date = new Date();
for (let avail of this.availabilities) {
let dateStart = new Date(date.getFullYear(), date.getMonth(), date.getDate(), Number(avail.start.split(':')[0]), Number(avail.start.split(':')[1]), 0);
let dateEnd = new Date(date.getFullYear(), date.getMonth(), date.getDate(), Number(avail.end.split(':')[0]), Number(avail.end.split(':')[1]), 0);
let availabilityTime = new Array<string>();
let time: number = dateStart.getTime();
while(time <= dateEnd.getTime()) {
let dateIn = new Date(time);
availabilityTime.push((dateIn.getHours()<10?('0'+dateIn.getHours()):String(dateIn.getHours())) +':'+ (dateIn.getMinutes()<10?('0'+dateIn.getMinutes()):String(dateIn.getMinutes())) +':00');
time = time + 3600000;
}
javascript typescript ionic-framework ionic3
javascript typescript ionic-framework ionic3
edited Jan 8 at 18:14
MathKimRobin
65611134
65611134
asked Jan 2 at 18:07
AbdullahAbdullah
419
419
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Get date as string format, split to parts.
hours = hours % 12 + (hours < 13 ? ' AM' : ' PM');
join
where i put this code
– Abdullah
Jan 2 at 18:31
add a comment |
You must use pipe for reusability. Create new typescript pipe with below code
import { Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';
@Pipe({
name: 'dateTime'
})
export class DateTimePipe implements PipeTransform {
transform(value: any, format: string = ''): string {
// Try and parse the passed value.
const momentDate = moment(value);
// If moment didn't understand the value, return it unformatted.
if (!momentDate.isValid()) return value;
// Otherwise, return the date formatted as requested.
return momentDate.format(format);
}
}
and in HTML use this pipe
<td>{{YourDateField.Date |dateTime :'MM/DD/YYYY hh:mm:ss A'}}</td>
i cant import moment
– Abdullah
Jan 2 at 22:33
1
npm install --save moment
– ashishsingh92
Jan 3 at 7:51
i got this error The pipe 'dateTime' could not be found (""selectedTime(time)"> <ion-card> <span class="center">{{[ERROR ->]time |dateTime :'MM/DD/YYYY hh:mm:ss A' }}</span> </ion-card> </i"): ng:///AppModule/Appointment_BookPage.html@22:47
– Abdullah
Jan 5 at 20:29
i have problem i did all this but no thing change i think the problem is from the pipe code
– Abdullah
Jan 8 at 0:20
Why can't you import moment ?
– MathKimRobin
Jan 8 at 16:12
add a comment |
thanks to all and i got the answer and here what i did for any one who need it
first install the moment
the i create pipe by : ionic g pipe MyPipe
then i add this code in the MyPipe file
import { Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';
/**
* Generated class for the MyPipe pipe.
*
* See https://angular.io/docs/ts/latest/guide/pipes.html for more info on
* Angular Pipes.
*/
@Pipe({
name: 'my',
})
export class MyPipe implements PipeTransform {
transform(time: any): any {
let hour = (time.split(':'))[0]
let min = (time.split(':'))[1]
let part = hour > 12 ? 'م' : 'ص';
min = (min+'').length == 1 ? `0${min}` : min;
hour = hour > 12 ? hour - 12 : hour;
hour = (hour+'').length == 1 ? `0${hour}` : hour;
return `${hour}:${min} ${part}`
}
}
after that in app.mdoule.ts file you have to add MyPipe under the declarations and providers
and last things is in your code add this in .ts file in constructor
private MyPipe:MyPipe
and in .html file
it will be like this
{{time |my: "hh:mm:ss"}}
MyPipe and my => you must change it by the name that you want when you generate the pipe
Maybe you should use a more explicit name for your pipe. "my" is not really meaningful
– MathKimRobin
Jan 8 at 17:03
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011121%2fconvert-24-hours-into-12-hours%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Get date as string format, split to parts.
hours = hours % 12 + (hours < 13 ? ' AM' : ' PM');
join
where i put this code
– Abdullah
Jan 2 at 18:31
add a comment |
Get date as string format, split to parts.
hours = hours % 12 + (hours < 13 ? ' AM' : ' PM');
join
where i put this code
– Abdullah
Jan 2 at 18:31
add a comment |
Get date as string format, split to parts.
hours = hours % 12 + (hours < 13 ? ' AM' : ' PM');
join
Get date as string format, split to parts.
hours = hours % 12 + (hours < 13 ? ' AM' : ' PM');
join
edited Jan 8 at 16:11
Vugar Dadalov
425
425
answered Jan 2 at 18:13
tomitheninjatomitheninja
878
878
where i put this code
– Abdullah
Jan 2 at 18:31
add a comment |
where i put this code
– Abdullah
Jan 2 at 18:31
where i put this code
– Abdullah
Jan 2 at 18:31
where i put this code
– Abdullah
Jan 2 at 18:31
add a comment |
You must use pipe for reusability. Create new typescript pipe with below code
import { Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';
@Pipe({
name: 'dateTime'
})
export class DateTimePipe implements PipeTransform {
transform(value: any, format: string = ''): string {
// Try and parse the passed value.
const momentDate = moment(value);
// If moment didn't understand the value, return it unformatted.
if (!momentDate.isValid()) return value;
// Otherwise, return the date formatted as requested.
return momentDate.format(format);
}
}
and in HTML use this pipe
<td>{{YourDateField.Date |dateTime :'MM/DD/YYYY hh:mm:ss A'}}</td>
i cant import moment
– Abdullah
Jan 2 at 22:33
1
npm install --save moment
– ashishsingh92
Jan 3 at 7:51
i got this error The pipe 'dateTime' could not be found (""selectedTime(time)"> <ion-card> <span class="center">{{[ERROR ->]time |dateTime :'MM/DD/YYYY hh:mm:ss A' }}</span> </ion-card> </i"): ng:///AppModule/Appointment_BookPage.html@22:47
– Abdullah
Jan 5 at 20:29
i have problem i did all this but no thing change i think the problem is from the pipe code
– Abdullah
Jan 8 at 0:20
Why can't you import moment ?
– MathKimRobin
Jan 8 at 16:12
add a comment |
You must use pipe for reusability. Create new typescript pipe with below code
import { Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';
@Pipe({
name: 'dateTime'
})
export class DateTimePipe implements PipeTransform {
transform(value: any, format: string = ''): string {
// Try and parse the passed value.
const momentDate = moment(value);
// If moment didn't understand the value, return it unformatted.
if (!momentDate.isValid()) return value;
// Otherwise, return the date formatted as requested.
return momentDate.format(format);
}
}
and in HTML use this pipe
<td>{{YourDateField.Date |dateTime :'MM/DD/YYYY hh:mm:ss A'}}</td>
i cant import moment
– Abdullah
Jan 2 at 22:33
1
npm install --save moment
– ashishsingh92
Jan 3 at 7:51
i got this error The pipe 'dateTime' could not be found (""selectedTime(time)"> <ion-card> <span class="center">{{[ERROR ->]time |dateTime :'MM/DD/YYYY hh:mm:ss A' }}</span> </ion-card> </i"): ng:///AppModule/Appointment_BookPage.html@22:47
– Abdullah
Jan 5 at 20:29
i have problem i did all this but no thing change i think the problem is from the pipe code
– Abdullah
Jan 8 at 0:20
Why can't you import moment ?
– MathKimRobin
Jan 8 at 16:12
add a comment |
You must use pipe for reusability. Create new typescript pipe with below code
import { Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';
@Pipe({
name: 'dateTime'
})
export class DateTimePipe implements PipeTransform {
transform(value: any, format: string = ''): string {
// Try and parse the passed value.
const momentDate = moment(value);
// If moment didn't understand the value, return it unformatted.
if (!momentDate.isValid()) return value;
// Otherwise, return the date formatted as requested.
return momentDate.format(format);
}
}
and in HTML use this pipe
<td>{{YourDateField.Date |dateTime :'MM/DD/YYYY hh:mm:ss A'}}</td>
You must use pipe for reusability. Create new typescript pipe with below code
import { Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';
@Pipe({
name: 'dateTime'
})
export class DateTimePipe implements PipeTransform {
transform(value: any, format: string = ''): string {
// Try and parse the passed value.
const momentDate = moment(value);
// If moment didn't understand the value, return it unformatted.
if (!momentDate.isValid()) return value;
// Otherwise, return the date formatted as requested.
return momentDate.format(format);
}
}
and in HTML use this pipe
<td>{{YourDateField.Date |dateTime :'MM/DD/YYYY hh:mm:ss A'}}</td>
answered Jan 2 at 18:54
ashishsingh92ashishsingh92
751115
751115
i cant import moment
– Abdullah
Jan 2 at 22:33
1
npm install --save moment
– ashishsingh92
Jan 3 at 7:51
i got this error The pipe 'dateTime' could not be found (""selectedTime(time)"> <ion-card> <span class="center">{{[ERROR ->]time |dateTime :'MM/DD/YYYY hh:mm:ss A' }}</span> </ion-card> </i"): ng:///AppModule/Appointment_BookPage.html@22:47
– Abdullah
Jan 5 at 20:29
i have problem i did all this but no thing change i think the problem is from the pipe code
– Abdullah
Jan 8 at 0:20
Why can't you import moment ?
– MathKimRobin
Jan 8 at 16:12
add a comment |
i cant import moment
– Abdullah
Jan 2 at 22:33
1
npm install --save moment
– ashishsingh92
Jan 3 at 7:51
i got this error The pipe 'dateTime' could not be found (""selectedTime(time)"> <ion-card> <span class="center">{{[ERROR ->]time |dateTime :'MM/DD/YYYY hh:mm:ss A' }}</span> </ion-card> </i"): ng:///AppModule/Appointment_BookPage.html@22:47
– Abdullah
Jan 5 at 20:29
i have problem i did all this but no thing change i think the problem is from the pipe code
– Abdullah
Jan 8 at 0:20
Why can't you import moment ?
– MathKimRobin
Jan 8 at 16:12
i cant import moment
– Abdullah
Jan 2 at 22:33
i cant import moment
– Abdullah
Jan 2 at 22:33
1
1
npm install --save moment
– ashishsingh92
Jan 3 at 7:51
npm install --save moment
– ashishsingh92
Jan 3 at 7:51
i got this error The pipe 'dateTime' could not be found (""selectedTime(time)"> <ion-card> <span class="center">{{[ERROR ->]time |dateTime :'MM/DD/YYYY hh:mm:ss A' }}</span> </ion-card> </i"): ng:///AppModule/Appointment_BookPage.html@22:47
– Abdullah
Jan 5 at 20:29
i got this error The pipe 'dateTime' could not be found (""selectedTime(time)"> <ion-card> <span class="center">{{[ERROR ->]time |dateTime :'MM/DD/YYYY hh:mm:ss A' }}</span> </ion-card> </i"): ng:///AppModule/Appointment_BookPage.html@22:47
– Abdullah
Jan 5 at 20:29
i have problem i did all this but no thing change i think the problem is from the pipe code
– Abdullah
Jan 8 at 0:20
i have problem i did all this but no thing change i think the problem is from the pipe code
– Abdullah
Jan 8 at 0:20
Why can't you import moment ?
– MathKimRobin
Jan 8 at 16:12
Why can't you import moment ?
– MathKimRobin
Jan 8 at 16:12
add a comment |
thanks to all and i got the answer and here what i did for any one who need it
first install the moment
the i create pipe by : ionic g pipe MyPipe
then i add this code in the MyPipe file
import { Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';
/**
* Generated class for the MyPipe pipe.
*
* See https://angular.io/docs/ts/latest/guide/pipes.html for more info on
* Angular Pipes.
*/
@Pipe({
name: 'my',
})
export class MyPipe implements PipeTransform {
transform(time: any): any {
let hour = (time.split(':'))[0]
let min = (time.split(':'))[1]
let part = hour > 12 ? 'م' : 'ص';
min = (min+'').length == 1 ? `0${min}` : min;
hour = hour > 12 ? hour - 12 : hour;
hour = (hour+'').length == 1 ? `0${hour}` : hour;
return `${hour}:${min} ${part}`
}
}
after that in app.mdoule.ts file you have to add MyPipe under the declarations and providers
and last things is in your code add this in .ts file in constructor
private MyPipe:MyPipe
and in .html file
it will be like this
{{time |my: "hh:mm:ss"}}
MyPipe and my => you must change it by the name that you want when you generate the pipe
Maybe you should use a more explicit name for your pipe. "my" is not really meaningful
– MathKimRobin
Jan 8 at 17:03
add a comment |
thanks to all and i got the answer and here what i did for any one who need it
first install the moment
the i create pipe by : ionic g pipe MyPipe
then i add this code in the MyPipe file
import { Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';
/**
* Generated class for the MyPipe pipe.
*
* See https://angular.io/docs/ts/latest/guide/pipes.html for more info on
* Angular Pipes.
*/
@Pipe({
name: 'my',
})
export class MyPipe implements PipeTransform {
transform(time: any): any {
let hour = (time.split(':'))[0]
let min = (time.split(':'))[1]
let part = hour > 12 ? 'م' : 'ص';
min = (min+'').length == 1 ? `0${min}` : min;
hour = hour > 12 ? hour - 12 : hour;
hour = (hour+'').length == 1 ? `0${hour}` : hour;
return `${hour}:${min} ${part}`
}
}
after that in app.mdoule.ts file you have to add MyPipe under the declarations and providers
and last things is in your code add this in .ts file in constructor
private MyPipe:MyPipe
and in .html file
it will be like this
{{time |my: "hh:mm:ss"}}
MyPipe and my => you must change it by the name that you want when you generate the pipe
Maybe you should use a more explicit name for your pipe. "my" is not really meaningful
– MathKimRobin
Jan 8 at 17:03
add a comment |
thanks to all and i got the answer and here what i did for any one who need it
first install the moment
the i create pipe by : ionic g pipe MyPipe
then i add this code in the MyPipe file
import { Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';
/**
* Generated class for the MyPipe pipe.
*
* See https://angular.io/docs/ts/latest/guide/pipes.html for more info on
* Angular Pipes.
*/
@Pipe({
name: 'my',
})
export class MyPipe implements PipeTransform {
transform(time: any): any {
let hour = (time.split(':'))[0]
let min = (time.split(':'))[1]
let part = hour > 12 ? 'م' : 'ص';
min = (min+'').length == 1 ? `0${min}` : min;
hour = hour > 12 ? hour - 12 : hour;
hour = (hour+'').length == 1 ? `0${hour}` : hour;
return `${hour}:${min} ${part}`
}
}
after that in app.mdoule.ts file you have to add MyPipe under the declarations and providers
and last things is in your code add this in .ts file in constructor
private MyPipe:MyPipe
and in .html file
it will be like this
{{time |my: "hh:mm:ss"}}
MyPipe and my => you must change it by the name that you want when you generate the pipe
thanks to all and i got the answer and here what i did for any one who need it
first install the moment
the i create pipe by : ionic g pipe MyPipe
then i add this code in the MyPipe file
import { Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';
/**
* Generated class for the MyPipe pipe.
*
* See https://angular.io/docs/ts/latest/guide/pipes.html for more info on
* Angular Pipes.
*/
@Pipe({
name: 'my',
})
export class MyPipe implements PipeTransform {
transform(time: any): any {
let hour = (time.split(':'))[0]
let min = (time.split(':'))[1]
let part = hour > 12 ? 'م' : 'ص';
min = (min+'').length == 1 ? `0${min}` : min;
hour = hour > 12 ? hour - 12 : hour;
hour = (hour+'').length == 1 ? `0${hour}` : hour;
return `${hour}:${min} ${part}`
}
}
after that in app.mdoule.ts file you have to add MyPipe under the declarations and providers
and last things is in your code add this in .ts file in constructor
private MyPipe:MyPipe
and in .html file
it will be like this
{{time |my: "hh:mm:ss"}}
MyPipe and my => you must change it by the name that you want when you generate the pipe
edited Jan 9 at 1:20
answered Jan 8 at 14:17
AbdullahAbdullah
419
419
Maybe you should use a more explicit name for your pipe. "my" is not really meaningful
– MathKimRobin
Jan 8 at 17:03
add a comment |
Maybe you should use a more explicit name for your pipe. "my" is not really meaningful
– MathKimRobin
Jan 8 at 17:03
Maybe you should use a more explicit name for your pipe. "my" is not really meaningful
– MathKimRobin
Jan 8 at 17:03
Maybe you should use a more explicit name for your pipe. "my" is not really meaningful
– MathKimRobin
Jan 8 at 17:03
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011121%2fconvert-24-hours-into-12-hours%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown