简体   繁体   English

iOS - 使用没有时区计算的 stringDate 初始化 NSDate

[英]iOS - Init NSDate with a stringDate without timezone calculations

I am getting a date time from web service which is in UTC format.我从UTC格式的Web 服务获取日期时间。 Now i want to assign it to NSDate.现在我想将它分配给 NSDate。 Following is the code i have done以下是我所做的代码

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];

NSDate *utcDate = [dateFormatter dateFromString:@"2015-08-18T23:00:00"];

but it is calculating its timezone calculations by default the result is 2015-08-19 03:00:00 +0000 .但它默认计算其时区计算结果是2015-08-19 03:00:00 +0000 How can i initialize NSDate with same date and Time.如何使用相同的日期和时间初始化 NSDate。 I want to perform timezone calculations later on我想稍后执行时区计算

edit/update:编辑/更新:

Xcode 11 • Swift 5.1 : Xcode 11 • Swift 5.1

let dateString = "2015-08-18T23:00:00"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
if let dateFromString = formatter.date(from: dateString) {
    print(dateFromString)   // "2015-08-18 23:00:00 +0000"
}

A default-allocated NSDateFormatter will use the current locale (the one that the user set up in Settings.app).默认分配的NSDateFormatter将使用当前区域设置(用户在 Settings.app 中设置的区域设置)。

You have to explicitly set a suitable locale on the formatter:您必须在格式化程序上明确设置合适的语言环境:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

This locale uses the gregorian calendar on UTC without any DST adjustments.此语言环境使用 UTC 上的公历,没有任何 DST 调整。

Edit: LeoDabus points out that setting the locale does not change the timezone of the date formatter.编辑:LeoDabus 指出设置locale不会更改日期格式化程序的timezone This has to be done explicitly:这必须明确地完成:

dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)

NSString *dateStr =[NSString stringWithFormat:@"%@",[dict valueForKey:@"utc_datetime"]]; NSString *dateStr =[NSString stringWithFormat:@"%@",[dict valueForKey:@"utc_datetime"]];

NSString* input = dateStr;
NSString* format = @"dd MMM yyyy - HH:mm:ss'";

// Set up an NSDateFormatter for UTC time zone
NSDateFormatter* formatterUtc = [[NSDateFormatter alloc] init];
[formatterUtc setDateFormat:format];
[formatterUtc setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

// Cast the input string to NSDate
NSDate* utcDate = [formatterUtc dateFromString:input];

// Set up an NSDateFormatter for the device's local time zone
NSDateFormatter* formatterLocal = [[NSDateFormatter alloc] init];
[formatterLocal setDateFormat:format];
[formatterLocal setTimeZone:[NSTimeZone localTimeZone]];

// Create local NSDate with time zone difference
NSDate* localDate = [formatterUtc dateFromString:[formatterLocal stringFromDate:utcDate]];
NSLog(@"utc:   %@", utcDate);
NSLog(@"local: %@", localDate);

NSDateFormatter *format1q = [[NSDateFormatter alloc] init];
format1q.dateFormat = @"HH:mm";


NSString *StrDAte=[NSString stringWithFormat:@"%@",[format1q stringFromDate:utcDate]];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"HH:mm";
NSDate *date = [dateFormatter dateFromString:StrDAte];
date = [date dateByAddingTimeInterval:-60];

dateFormatter.dateFormat = @"hh:mm a";
NSString *pmamDateString = [dateFormatter stringFromDate:date];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM