Objective-C 如何将日期字符串转换为另一种格式
How to convert date string to another format in Objective-C
我有一个日期选择器,它只为那个日期添加日期我想添加时间字符串并将其转换为日期。
fromString = @"00:00:00";
NSString *fromDate = [fromdate.text stringByAppendingString:@" "];
fromDate = [fromDate fromString];
NSDate *currentDate = [self dateFromString:fromDate withFormat:@"dd/MM/yyyy HH:mm:ss"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+5.30"]];
NSString *dateString = [dateFormatter stringFromDate: currentDate];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSDate *date = [self dateFromString: dateString withFormat:@"dd/MM/yyyy HH:mm:ss"];
NSString *fromDate1 = [NSString stringWithFormat:@"/Date(%.0f000%@)/", [date timeIntervalSince1970],[formatter stringFromDate:date]];
我的问题是我在转换为字符串时获得了 5.30 的额外时间。
NSString *finalDate = ……
NSDateFormatter *dateFormatterTime = [[NSDateFormatter alloc] init];
NSLocale *location = [NSLocale currentLocale];
NSString *country = [location localeIdentifier];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:country];
[dateFormatterTime setLocale:locale];
[dateFormatterTime setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *timeZone = [dateFormatterTime dateFromString:finalDate];
[dateFormatterTime setDateFormat:@"yyyy-MM-dd hh:mm:ss a"];
NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"EST"]; // set as you wish
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:timeZone];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:timeZone];
NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;
// get final date in LocalTimeZone
NSDate *destinationDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:timeZone];
NSLog(@“你的 NSDate = %@”, destinationDate);
My Problem is that I'm getting 5.30 extra time when converting to string.
要从最初使用的字符串转换:
NSDate *CurrentDate = [self dateFromString:fROMDATE withFormat:@"dd/MM/yyyy HH:mm:ss"];
您没有提供 dateFromString
的代码,但猜测此例程使用 GMT/UTC 时区进行转换。
要转换回您首先执行的字符串:
[DATEFORMATER setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+5.30"]];
因此,如果您的原始时间是从字符串转换为 GMT,您的 DATE_STRING
将显示 5.5 小时的时间 "later"(与 GMT 相同的时间点,只是显示在GMT+5.30 时区)。
HTH
我有一个日期选择器,它只为那个日期添加日期我想添加时间字符串并将其转换为日期。
fromString = @"00:00:00";
NSString *fromDate = [fromdate.text stringByAppendingString:@" "];
fromDate = [fromDate fromString];
NSDate *currentDate = [self dateFromString:fromDate withFormat:@"dd/MM/yyyy HH:mm:ss"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+5.30"]];
NSString *dateString = [dateFormatter stringFromDate: currentDate];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSDate *date = [self dateFromString: dateString withFormat:@"dd/MM/yyyy HH:mm:ss"];
NSString *fromDate1 = [NSString stringWithFormat:@"/Date(%.0f000%@)/", [date timeIntervalSince1970],[formatter stringFromDate:date]];
我的问题是我在转换为字符串时获得了 5.30 的额外时间。
NSString *finalDate = ……
NSDateFormatter *dateFormatterTime = [[NSDateFormatter alloc] init];
NSLocale *location = [NSLocale currentLocale];
NSString *country = [location localeIdentifier];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:country];
[dateFormatterTime setLocale:locale];
[dateFormatterTime setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *timeZone = [dateFormatterTime dateFromString:finalDate];
[dateFormatterTime setDateFormat:@"yyyy-MM-dd hh:mm:ss a"];
NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"EST"]; // set as you wish
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:timeZone];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:timeZone];
NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;
// get final date in LocalTimeZone
NSDate *destinationDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:timeZone];
NSLog(@“你的 NSDate = %@”, destinationDate);
My Problem is that I'm getting 5.30 extra time when converting to string.
要从最初使用的字符串转换:
NSDate *CurrentDate = [self dateFromString:fROMDATE withFormat:@"dd/MM/yyyy HH:mm:ss"];
您没有提供 dateFromString
的代码,但猜测此例程使用 GMT/UTC 时区进行转换。
要转换回您首先执行的字符串:
[DATEFORMATER setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+5.30"]];
因此,如果您的原始时间是从字符串转换为 GMT,您的 DATE_STRING
将显示 5.5 小时的时间 "later"(与 GMT 相同的时间点,只是显示在GMT+5.30 时区)。
HTH