gpt4 book ai didi

ios - 使用NSDateFormatter转换NSDate

转载 作者:行者123 更新时间:2023-12-01 19:00:25 28 4
gpt4 key购买 nike

我有一组日期为NSString。它们采用以下格式:1980/9/26 17:23:00。问题在于,从1到9的月份中,月份不包含零。因此,某些日期为1980/9/26 17:23:00,而某些日期为1980/12/26 17:23:00。日期格式化程序将获得yyyy/mm/dd hh:mm:ssyyyy/m/dd hh:mm:ss

无论我使用这两种格式中的哪一种,我都无法格式化两种日期类型。这是我使用的代码:

-(NSString *) convertDate : (NSString *) unformatted
{

//Format the Date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy/mm/dd hh:mm:ss"];

NSDate *date = [dateFormat dateFromString:unformatted];

//Format the Calendar
NSDateFormatter *f = [[NSDateFormatter alloc] init];
NSCalendar *persian = [[NSCalendar alloc] initWithCalendarIdentifier:@"persian"];
[f setCalendar:persian];
[f setDateStyle:NSDateFormatterLongStyle];

NSString *formattedDate = [f stringFromDate:date];
//Replace english strings with farsi strings
formattedDate=[formattedDate stringByReplacingOccurrencesOfString:@"Farvardin" withString:@"فروردین"];
formattedDate=[formattedDate stringByReplacingOccurrencesOfString:@"Ordibehesht" withString:@"اردیبهشت"];
formattedDate=[formattedDate stringByReplacingOccurrencesOfString:@"Khordad" withString:@"خرداد"];
formattedDate=[formattedDate stringByReplacingOccurrencesOfString:@"Tir" withString:@"تیر"];
formattedDate=[formattedDate stringByReplacingOccurrencesOfString:@"Mordad" withString:@"مرداد"];
formattedDate=[formattedDate stringByReplacingOccurrencesOfString:@"Shahrivar" withString:@"شهریور"];
formattedDate=[formattedDate stringByReplacingOccurrencesOfString:@"Mehr" withString:@"مهر"];
formattedDate=[formattedDate stringByReplacingOccurrencesOfString:@"Aban" withString:@"آبان"];
formattedDate=[formattedDate stringByReplacingOccurrencesOfString:@"Azar" withString:@"آذر"];
formattedDate=[formattedDate stringByReplacingOccurrencesOfString:@"Dey" withString:@"دی"];
formattedDate=[formattedDate stringByReplacingOccurrencesOfString:@"Bahman" withString:@"بهمن"];
formattedDate=[formattedDate stringByReplacingOccurrencesOfString:@"Esfand" withString:@"اسفند"];
formattedDate=[formattedDate stringByReplacingOccurrencesOfString:@"AP" withString:@""];

return formattedDate;
}

最佳答案

您需要使用MM数月。另外,您需要使用HH数小时。尝试使用以下格式:yyyy/MM/dd HH:mm:ss

关于ios - 使用NSDateFormatter转换NSDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23322611/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com