gpt4 book ai didi

ios - 如何在 IOS 中将日期格式化为字符串,如 "One Days Ago","Minutes Ago"?

转载 作者:IT王子 更新时间:2023-10-29 08:21:40 25 4
gpt4 key购买 nike

我创建了一个包含 JSON 解析数据的应用程序,这里是我的 JSON 解析数据,其中包含类似“2014-12-02 08:00:42”的日期,然后我将此日期转换为以下格式“2014 年 2 月 12 日”,如下所示

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString *date=[dict valueForKey:@"post_date"];
NSDate * dateNotFormatted = [dateFormatter dateFromString:date];
[dateFormatter setDateFormat:@"d MMM YYYY"];
NSString * dateFormatted = [dateFormatter stringFromDate:dateNotFormatted];
cell.timeLabel.text=[dateFormatted uppercaseString];

它工作正常,但现在我想将此 dateFormatted 字符串转换为“一天前”、“分钟前”。我知道这个问题以前被问过很多次。

最佳答案

从 iOS 8 开始,您可以通过使用 NSDateComponentsFormatter 获得很多帮助(包括本地化)。注意:如果您想返回类似“2 天、3 小时、5 分钟前”的字符串,您可以通过更改组件格式化程序上的 allowedUnits 轻松更改字符串显示的粒度。

swift 4:

func timeAgoStringFromDate(date: Date) -> String? {
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .full

let now = Date()

let calendar = NSCalendar.current
let components1: Set<Calendar.Component> = [.year, .month, .weekOfMonth, .day, .hour, .minute, .second]
let components = calendar.dateComponents(components1, from: date, to: now)

if components.year ?? 0 > 0 {
formatter.allowedUnits = .year
} else if components.month ?? 0 > 0 {
formatter.allowedUnits = .month
} else if components.weekOfMonth ?? 0 > 0 {
formatter.allowedUnits = .weekOfMonth
} else if components.day ?? 0 > 0 {
formatter.allowedUnits = .day
} else if components.hour ?? 0 > 0 {
formatter.allowedUnits = [.hour]
} else if components.minute ?? 0 > 0 {
formatter.allowedUnits = .minute
} else {
formatter.allowedUnits = .second
}

let formatString = NSLocalizedString("%@ left", comment: "Used to say how much time has passed. e.g. '2 hours ago'")

guard let timeString = formatter.string(for: components) else {
return nil
}
return String(format: formatString, timeString)
}

let str = timeAgoStringFromDate(date: Date().addingTimeInterval(-11000))
// Result: "3 hours, 3 minutes left"

swift :

class func timeAgoStringFromDate(date: NSDate) -> NSString? {
let formatter = NSDateComponentsFormatter()
formatter.unitsStyle = .Full

let now = NSDate()

let calendar = NSCalendar.currentCalendar()
let components = calendar.components([NSCalendarUnit.Year, .Month, .WeekOfMonth, .Day, .Hour, .Minute, .Second],
fromDate: date,
toDate: now,
options:NSCalendarOptions(rawValue: 0))

if components.year > 0 {
formatter.allowedUnits = .Year
} else if components.month > 0 {
formatter.allowedUnits = .Month
} else if components.weekOfMonth > 0 {
formatter.allowedUnits = .WeekOfMonth
} else if components.day > 0 {
formatter.allowedUnits = .Day
} else if components.hour > 0 {
formatter.allowedUnits = .Hour
} else if components.minute > 0 {
formatter.allowedUnits = .Minute
} else {
formatter.allowedUnits = .Second
}

let formatString = NSLocalizedString("%@ ago", comment: "Used to say how much time has passed. e.g. '2 hours ago'")

guard let timeString = formatter.stringFromDateComponents(components) else {
return nil
}
return String(format: formatString, timeString)
}

objective-C :

+ (NSString *)timeAgoStringFromDate:(NSDate *)date {
NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleFull;

NSDate *now = [NSDate date];

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitWeekOfMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond)
fromDate:date
toDate:now
options:0];

if (components.year > 0) {
formatter.allowedUnits = NSCalendarUnitYear;
} else if (components.month > 0) {
formatter.allowedUnits = NSCalendarUnitMonth;
} else if (components.weekOfMonth > 0) {
formatter.allowedUnits = NSCalendarUnitWeekOfMonth;
} else if (components.day > 0) {
formatter.allowedUnits = NSCalendarUnitDay;
} else if (components.hour > 0) {
formatter.allowedUnits = NSCalendarUnitHour;
} else if (components.minute > 0) {
formatter.allowedUnits = NSCalendarUnitMinute;
} else {
formatter.allowedUnits = NSCalendarUnitSecond;
}

NSString *formatString = NSLocalizedString(@"%@ ago", @"Used to say how much time has passed. e.g. '2 hours ago'");

return [NSString stringWithFormat:formatString, [formatter stringFromDateComponents:components]];
}

关于ios - 如何在 IOS 中将日期格式化为字符串,如 "One Days Ago","Minutes Ago"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27519533/

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