【问题标题】:How to get the current time based on TimeZone in objective c?如何根据目标c中的TimeZone获取当前时间?
【发布时间】:2018-04-09 19:41:57
【问题描述】:

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

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

【问题讨论】:

标签: ios objective-c iphone


【解决方案1】:

试试这个..

    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);

【讨论】:

    猜你喜欢
    • 2019-07-21
    • 2014-09-29
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-13
    相关资源
    最近更新 更多