如何检测设备的时间设置?

发布于 2024-12-14 04:30:56 字数 213 浏览 1 评论 0原文

我有一个应用程序,其中我制作了一个闹钟。在我的应用程序中,问题是当设备日期和时间设置更改时,警报标签的文本也会更改。例如,如果时间设置设置为 24 小时,那么在我的标签文本中最后不会显示上午和下午,而当将设置更改为 12 小时时,则在标签文本中显示上午和下午。如何检测日期和时间的设置是什么,以及如何对标签文本应用验证,使任何设置始终以上午和下午模式显示或以 12 小时设置显示?

预先感谢...

I have an application in which i have made a alarm clock. In my application problem is that when device date and time settings changed then text of label of alarm also changed. For example if time settings is set as 24 hours then in my label 's text don't show am and pm in last while when change settings as 12 hours then in label's text show am and pm. How detect that what is settings of date and time and how apply validation on label's text that whatever settings always display in am and pm mode or show in 12 hours settings?

Thanks in advances...

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

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

发布评论

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

评论(2

通知家属抬走 2024-12-21 04:30:57
Use can use this code:
NSDate *today = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    // display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings

    [dateFormatter setDateFormat:@"HH:mm"];
    NSString *currentTime = [dateFormatter stringFromDate:today];
    NSLog(@"%@",currentTime);
    [dateFormatter release];
Hear currentTime contains always 24 hour time format.

int hour =  [[[currentTime componentsSeparatedByString:@":"] objectAtIndex:0] intValue];
  if(hour > 12){
//show PM
}else{
//Show AM
}
Use can use this code:
NSDate *today = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    // display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings

    [dateFormatter setDateFormat:@"HH:mm"];
    NSString *currentTime = [dateFormatter stringFromDate:today];
    NSLog(@"%@",currentTime);
    [dateFormatter release];
Hear currentTime contains always 24 hour time format.

int hour =  [[[currentTime componentsSeparatedByString:@":"] objectAtIndex:0] intValue];
  if(hour > 12){
//show PM
}else{
//Show AM
}
梅倚清风 2024-12-21 04:30:57

只需放置此代码

[dateFormatter setDateFormat:@"hh:mm a"];

而不是此代码

[dateFormatter setDateFormat:@"HH:mm"];

,然后

NSString *currentTime = [dateFormatter stringFromDate:today];
NSLog(@"%@",currentTime);

NSLog 将分别使用 AM 或 PM 打印当前时间 12 小时格式

just place this code

[dateFormatter setDateFormat:@"hh:mm a"];

instead of this

[dateFormatter setDateFormat:@"HH:mm"];

then

NSString *currentTime = [dateFormatter stringFromDate:today];
NSLog(@"%@",currentTime);

NSLog will print the current time 12hour format with AM or PM respectively

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