作者:李da寕 | 来源:互联网 | 2023-07-30 15:31
我有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格式。
您的代码正确无误,我添加了defaultTimeZone以获取时区,并将时间减去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;
}