我有 4 个 UITextfileds,开始日期、开始时间和结束日期、结束时间。然后我将日期和时间发送到我的 UTC 函数中,但它返回 null。
NSString *str1 = _startDateField.text;
str1 = [str1 stringByAppendingString:@" "];
str1 = [str1 stringByAppendingString:_startTimeField.text];
NSLog(@"str1 : %@", str1);
NSString *str2 = _endDateField.text;
str2 = [str2 stringByAppendingString:@" "];
str2 = [str2 stringByAppendingString:_endTimeField.text];
NSLog(@"str2 : %@", str2);
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd-MMM-yyyy HH:mm"];
NSDate *date1 = [dateFormat dateFromString:str1];
NSDate *date2 = [dateFormat dateFromString:str2];
NSLog(@"%@", date1);
NSLog(@"%@", date2);
NSString *startTime = [self getUTCFormateDate:date1];
NSString *endTime = [self getUTCFormateDate:date2];
NSLog(@"str1 : %@", startTime);
NSLog(@"str2 : %@", endTime);
覆盖UTC
格式:
-(NSString *)getUTCFormateDate:(NSDate *)localDate {
NSLog(@"%@", localDate);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"dd-MMM-yyyy HH:mm"];
NSString *dateString = [dateFormatter stringFromDate:localDate];
NSLog(@"%@", dateString);
return dateString;
}
我想从文本字段中读取日期和时间,并将它们转换为 UTC 格式。
您的代码很好且正确,我添加了默认时区以获取时区并将时间减去UTC时间。我得到了输出。
检查这个
NSString *str1 = @"26-Nov-2019";
str1 = [str1 stringByAppendingString:@" "];
str1 = [str1 stringByAppendingString:@"9:35 am"];
NSLog(@"str1 : %@", str1);
NSString *str2 = @"26-Nov-2019";
str2 = [str2 stringByAppendingString:@" "];
str2 = [str2 stringByAppendingString:@"11:59"];
NSLog(@"str2 : %@", str2);
// Convert string to date object
NSDate *date1 = [self getUTCDate:str1];
NSDate *date2 = [self getUTCDate:str2];
NSLog(@"date1 %@", date1);
NSLog(@"date2 %@", date2);
NSString *startTime = [self getUTCFormateDate:date1];
NSString *endTime = [self getUTCFormateDate:date2];
NSLog(@"str1 : %@", startTime);
NSLog(@"str2 : %@", endTime);
-(NSDate *)getUTCDate:(NSString *)currentDate {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd-MMM-yyyy HH:mm"];
NSDate *date1 = [dateFormat dateFromString:currentDate];
if (date1 == nil) {
[dateFormat setDateFormat:@"dd-MMM-yyyy hh:mm a"];
date1 = [dateFormat dateFromString:currentDate];
}
return date1;
}
-(NSString *)getUTCFormateDate:(NSDate *)localDate {
NSLog(@"%@", localDate);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"dd-MMM-yyyy HH:mm"];
NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
dateFormatter.locale = twelveHourLocale;
NSTimeInterval timeZoneoffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
NSTimeInterval utcTimeInterval = [localDate timeIntervalSinceReferenceDate] - timeZoneoffset;
NSDate *utcCurrentDate = [NSDate dateWithTimeIntervalSinceReferenceDate:utcTimeInterval];
NSString *dateString = [dateFormatter stringFromDate:utcCurrentDate];
NSLog(@"dateString %@", dateString);
return dateString;
}