如何在Objective-C中将日期字符串转换为其他格式

如何在Objective-C中将日期字符串转换为其他格式,objective-c,nsdateformatter,date-formatting,Objective C,Nsdateformatter,Date Formatting,我有一个日期选择器,它只为那个日期添加日期,我想添加时间字符串并将其转换为日期 fromString = @"00:00:00"; NSString *fromDate = [fromdate.text stringByAppendingString:@" "]; fromDate = [fromDate fromString]; NSDate *currentDate = [self dateFromString:fromDate withFormat:@"dd/MM/yyyy HH:mm:s

我有一个日期选择器,它只为那个日期添加日期,我想添加时间字符串并将其转换为日期

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)

我的问题是,在转换为字符串时,我得到了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];
要最初从字符串转换,请使用:

NSDate *CurrentDate = [self dateFromString:fROMDATE withFormat:@"dd/MM/yyyy HH:mm:ss"];
您没有给出
dateFromString
的代码,但猜测此例程正在使用GMT/UTC时区进行转换

要转换回字符串,请首先执行以下操作:

[DATEFORMATER setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+5.30"]];
因此,如果将原始时间从字符串转换为GMT,则
DATE\u字符串
将显示5.5小时后的时间(GMT中的时间点与GMT+5.30时区中的时间点相同)


HTH

代码中没有与JSON相关的内容。JSON没有日期格式。但是,我想您应该在格式化程序上设置零时区。不知道为什么要将日期转换为字符串和反之亦然三次。我已经编辑了您的代码,将变量名更改为符合标准命名约定,以便语法着色工作,使其更易于阅读。最好遵循这些约定。jsonDate:/Date(152260740000+0530)/我想要这个格式我不明白,请在代码中检查我的问题你在哪里将日期转换为Json字符串我想要转换后的字符串Hello Dinesh,现在检查上面的答案!jsonDate:/日期(152260740000+0530)/