如何在不同的文本字段中显示时间段?

发布于 2024-12-06 10:26:31 字数 620 浏览 0 评论 0原文

我有三个文本字段,我想在其中显示当前时间。在第一个文本字段中,它仅显示小时,在第二个文本字段中,它仅显示分钟,在最后一个文本字段中,它显示上午或下午。为了获取当前时间,我使用此代码,但如何根据我的要求显示? 代码...

- (void)viewDidLoad {

NSDate* date = [NSDate date];

//Create the dateformatter object

NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];

//Set the required date format

[formatter setDateFormat:@"HH:MM"];

//Get the string date

NSString* str = [formatter stringFromDate:date];

//Display on the console

NSLog(@"%@",str);

//Set in the lable

time_label.text=[NSString stringWithFormat:@"%@ AM",str];
[super viewDidLoad];

}

提前致谢...

I have three text field in which i want to show the current time. In the first text field,it shows only hours , in second text field it shows only minute and in last one it shows am or pm. For getting current time I use this code but how do I show according to my requirement?
code...

- (void)viewDidLoad {

NSDate* date = [NSDate date];

//Create the dateformatter object

NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];

//Set the required date format

[formatter setDateFormat:@"HH:MM"];

//Get the string date

NSString* str = [formatter stringFromDate:date];

//Display on the console

NSLog(@"%@",str);

//Set in the lable

time_label.text=[NSString stringWithFormat:@"%@ AM",str];
[super viewDidLoad];

}

Thanks in advance...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜唯美灬不弃 2024-12-13 10:26:31
NSDate* date = [NSDate date];

//Create the dateformatter object

NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];

//Set the required date format

[formatter setDateFormat:@"hh MM a"];

//Get the string date

NSString* str = [formatter stringFromDate:date];

//Display on the console

NSLog(@"%@",str);
NSMutableArray *Arr=[str componentsSeparatedByString:@" "];

textFeild1.text=[Arr objectAtIndex:0]; //hoursTextFeild
textFeild2.text=[Arr objectAtIndex:1]; //minutesTextFeild
textFeild3.text=[Arr objectAtIndex:2];//Am/Pm TextFeild
NSDate* date = [NSDate date];

//Create the dateformatter object

NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];

//Set the required date format

[formatter setDateFormat:@"hh MM a"];

//Get the string date

NSString* str = [formatter stringFromDate:date];

//Display on the console

NSLog(@"%@",str);
NSMutableArray *Arr=[str componentsSeparatedByString:@" "];

textFeild1.text=[Arr objectAtIndex:0]; //hoursTextFeild
textFeild2.text=[Arr objectAtIndex:1]; //minutesTextFeild
textFeild3.text=[Arr objectAtIndex:2];//Am/Pm TextFeild
北座城市 2024-12-13 10:26:31
- (void)viewDidLoad {

    NSDate* date = [NSDate date];

    //Create the dateformatter object

    NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];

    //Set the required date format

    [formatter setDateFormat:@"HH:MM"];

    //Get the string date

    NSString* str = [formatter stringFromDate:date];

    //Display on the console

    NSLog(@"%@",str);

    //Set in the label
   //seperate its components
    NSMutableArray *array = [str componentsSeperatedByString:@" "];

   [firstTextField setText:[array objectAtIndex:0]];
   [secondTextField setText:[array objectAtIndex:1]];
   [thirdTextField setText:@"AM/PM"];


    [super viewDidLoad];
    }
- (void)viewDidLoad {

    NSDate* date = [NSDate date];

    //Create the dateformatter object

    NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];

    //Set the required date format

    [formatter setDateFormat:@"HH:MM"];

    //Get the string date

    NSString* str = [formatter stringFromDate:date];

    //Display on the console

    NSLog(@"%@",str);

    //Set in the label
   //seperate its components
    NSMutableArray *array = [str componentsSeperatedByString:@" "];

   [firstTextField setText:[array objectAtIndex:0]];
   [secondTextField setText:[array objectAtIndex:1]];
   [thirdTextField setText:@"AM/PM"];


    [super viewDidLoad];
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文