gpt4 book ai didi

ios - 如何根据 objective-c 中的TimeZone获取当前时间?

转载 作者:可可西里 更新时间:2023-11-01 04:43:06 25 4
gpt4 key购买 nike

如何在 objective-c 中根据TimeZone获取当前时间?

示例-> 亚洲/加尔各答的当前时间。

最佳答案

试试这个..

    NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm"];

//Create the date assuming the given string is in GMT
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];

//Create a date string in the local timezone
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:[NSTimeZone localTimeZone].secondsFromGMT];
NSString *localDateString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"date = %@", localDateString);

如果你想要特定的时区日期,那么请看下面的代码..

NSDate *myDate = [NSDate date];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateStyle:NSDateFormatterLongStyle];
[dateFormatter1 setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter1 setDateFormat:@"dd/MM/yyy hh:mm a"];

NSTimeZone *timeZone = [NSTimeZone timeZoneWithName: @"Asia/Calcutta"];
[dateFormatter1 setTimeZone:timeZone];

NSString *dateString = [dateFormatter1 stringFromDate: myDate];
NSLog(@"%@", dateString);

关于ios - 如何根据 objective-c 中的TimeZone获取当前时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46987943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com