如何将日期格式化为字符串,就像IOS中的“One Days Ago”,“Minutes Ago”?

时间:2022-08-25 17:52:30

I make an Application that contains JSON parse data here is my JSON parse data containing a Date like "2014-12-02 08:00:42" then I convert this Date into following format "12 FEB 2014" like as

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

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];

It is working fine but now I want to convert this dateFormatted string into "One Day Ago","Minutes Ago". I know that this question is asked many times before.

它运行正常,但现在我想将这个dateFormatted字符串转换为“One Day Ago”,“Minutes Ago”。我知道这个问题之前已被问过很多次了。

11 个解决方案

#1


16  

As of iOS 8, you can get a lot of help (including localization) by using NSDateComponentsFormatter. Note: you can easily change how much granularity the string shows by changing the allowedUnits on the components formatter if you want to return a string like "2 days, 3 hours, 5 minutes ago".

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

Swift 4:

斯威夫特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:

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]];
}

#2


7  

I used DateTools to achieve it. It supports Cocoapods installation.

我使用DateTools来实现它。它支持Cocoapods安装。

Works like so..

像这样工作..

NSDate *timeAgoDate = [NSDate dateWithTimeIntervalSinceNow:-4];
NSLog(@"Time Ago: %@", timeAgoDate.timeAgoSinceNow);
NSLog(@"Time Ago: %@", timeAgoDate.shortTimeAgoSinceNow);

//Output:
//Time Ago: 4 seconds ago
//Time Ago: 4s

taken from the Github page (https://github.com/MatthewYork/DateTools)

取自Github页面(https://github.com/MatthewYork/DateTools)

#3


6  

This function will return NSString starting from sec to years. Like if your date is of "1 sec ago" or if it is of "1 min ago" or "1 year ago" and so on.. it will return likewise..

此函数将从秒到年返回NSString。就像你的约会时间是“1秒前”,或者它是“1分钟前”或“1年前”等等......它会同样返回...

+(NSString*)HourCalculation:(NSString*)PostDate

{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    [dateFormat setTimeZone:gmt];
    NSDate *ExpDate = [dateFormat dateFromString:PostDate];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSDayCalendarUnit|NSWeekCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:ExpDate toDate:[NSDate date] options:0];
    NSString *time;
    if(components.year!=0)
    {
        if(components.year==1)
        {
            time=[NSString stringWithFormat:@"%ld year",(long)components.year];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld years",(long)components.year];
        }
    }
    else if(components.month!=0)
    {
        if(components.month==1)
        {
            time=[NSString stringWithFormat:@"%ld month",(long)components.month];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld months",(long)components.month];
        }
    }
    else if(components.week!=0)
    {
        if(components.week==1)
        {
            time=[NSString stringWithFormat:@"%ld week",(long)components.week];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld weeks",(long)components.week];
        }
    }
    else if(components.day!=0)
    {
        if(components.day==1)
        {
            time=[NSString stringWithFormat:@"%ld day",(long)components.day];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld days",(long)components.day];
        }
    }
    else if(components.hour!=0)
    {
        if(components.hour==1)
        {
            time=[NSString stringWithFormat:@"%ld hour",(long)components.hour];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld hours",(long)components.hour];
        }
    }
    else if(components.minute!=0)
    {
        if(components.minute==1)
        {
            time=[NSString stringWithFormat:@"%ld min",(long)components.minute];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld mins",(long)components.minute];
        }
    }
    else if(components.second>=0)
    {
        if(components.second==0)
        {
            time=[NSString stringWithFormat:@"1 sec"];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld secs",(long)components.second];
        }
    }
    return [NSString stringWithFormat:@"%@ ago",time];
}

#4


5  

Simplified implementation of @jDutton

简化了@jDutton的实现

swift4, swift3

swift4,swift3

extension Date {
   var timestampString: String? {
      let formatter = DateComponentsFormatter()
      formatter.unitsStyle = .full
      formatter.maximumUnitCount = 1
      formatter.allowedUnits = [.year, .month, .day, .hour, .minute, .second]

      guard let timeString = formatter.string(from: self, to: Date()) else {
           return nil
      }

      let formatString = NSLocalizedString("%@ ago", comment: "")
      return String(format: formatString, timeString)
   }
}

swift2

swift2

extension NSDate {
   var timestampString: String? {
      let formatter = NSDateComponentsFormatter()
      formatter.unitsStyle = .Full
      formatter.maximumUnitCount = 1
      formatter.allowedUnits = [.Year, .Month, .Day, .Hour, .Minute, .Second]

      guard let timeString = formatter.stringFromDate(self, toDate: NSDate()) else {
         return nil
      }

      let formatString = NSLocalizedString("%@ ago", comment: "")
      return String(format: formatString, timeString)
   }
}

#5


4  

One option is you can compare the current time and previous one and implement switch cases to get the string that you want.

一个选项是您可以比较当前时间和前一个时间并实现切换案例以获取所需的字符串。

Or you can use any of the following libraries:

或者您可以使用以下任何库:

  1. FormatterKit
  2. FormatterKit
  3. NSDate-Time-Ago
  4. NSDate的时间同期

I've created a library for swift, you can get it from here : Past

我已经为swift创建了一个库,你可以从这里得到它:过去

#6


4  

Swift 3, Xcode version 8.2.1:

Swift 3,Xcode版本8.2.1:

func elapsedTime () -> String
{
    //just to create a date that is before the current time
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    dateFormatter.locale = Locale.init(identifier: "en_GB")
    let before = dateFormatter.date(from: "2016-11-16 16:28:17")!

    //getting the current time
    let now = Date()

    let formatter = DateComponentsFormatter()
    formatter.unitsStyle = .full
    formatter.zeroFormattingBehavior = .dropAll
    formatter.maximumUnitCount = 1 //increase it if you want more precision
    formatter.allowedUnits = [.year, .month, .weekOfMonth, .day, .hour, .minute]
    formatter.includesApproximationPhrase = true //to write "About" at the beginning


    let formatString = NSLocalizedString("%@ ago", comment: "Used to say how much time has passed. e.g. '2 hours ago'")
    let timeString = formatter.string(from: before, to: now)
    return String(format: formatString, timeString!)
}

#7


4  

You can find also an example here: https://github.com/tneginareb/Time-Ago-iOS

你可以在这里找到一个例子:https://github.com/tneginareb/Time-Ago-iOS

You can modify the input variable "timeAtMiliseconds", for my example was at the format of date was at Milliseconds.

您可以修改输入变量“timeAtMiliseconds”,因为我的示例是日期格式为毫秒。

+(NSString *) parseDate: (long)dayago{

if(dayago == 0)
    return @"";

NSString *timeLength =[NSString stringWithFormat:@"%lu",dayago];
NSUInteger length = [timeLength length];
if(length == 13){
    dayago = dayago / 1000;

}

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *createdDate = [NSDate dateWithTimeIntervalSince1970:dayago];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
[dateFormatter setTimeZone:timeZone];
NSString *formattedDateString = [dateFormatter stringFromDate:createdDate];

if([self todaysIsLess:formattedDateString]){
    return @"";
}


NSString *timeLeft;
NSDate *currentDate =[NSDate date];
NSInteger seconds = [currentDate timeIntervalSinceDate:createdDate];




NSInteger days = (int) (floor(seconds / (3600 * 24)));
if(days) seconds -= days * 3600 * 24;

NSInteger hours = (int) (floor(seconds / 3600));
if(hours) seconds -= hours * 3600;

NSInteger minutes = (int) (floor(seconds / 60));
if(minutes) seconds -= minutes * 60;

if(days) {
    timeLeft = [NSString stringWithFormat:@"%ld Days", (long)days*-1];
}
else if(hours) { timeLeft = [NSString stringWithFormat: @"%ld H", (long)hours*-1];
}
else if(minutes) { timeLeft = [NSString stringWithFormat: @"%ld M", (long)minutes*-1];
}
else if(seconds)
    timeLeft = [NSString stringWithFormat: @"%lds", (long)seconds*-1];
//NSLog(@"Days: %lu <>  Hours: %lu <> Minutes: %lu  <> Seconds: %lu",days,hours,minutes,seconds);

NSString *result = [[NSString alloc]init];

if (days == 0) {
    if (hours == 0) {
        if (minutes == 0) {
            if (seconds < 0) {
                return @"0s";
            } else {
                if (seconds < 59) {
                    return @"now";
                }
            }
        } else {
            return  [NSString stringWithFormat:@"%lum",minutes];
        }
    } else {
        return  [NSString stringWithFormat:@"%luh",hours];
    }

} else {
    if (days <= 29) {
        return  [NSString stringWithFormat: @"%lud",days];
    }
    if (days > 29 && days <= 58) {
        return  @"1Mth";
    }
    if (days > 58 && days <= 87) {
        return  @"2Mth";
    }
    if (days > 87 && days <= 116) {
        return  @"3Mth";
    }
    if (days > 116 && days <= 145) {
        return  @"4Mth";
    }
    if (days > 145 && days <= 174) {
        return  @"5Mth";
    }
    if (days > 174 && days <= 203) {
        return  @"6Mth";
    }
    if (days > 203 && days <= 232) {
        return  @"7Mth";
    }
    if (days > 232 && days <= 261) {
        return  @"8Mth";
    }
    if (days > 261 && days <= 290) {
        return  @"9Mth";
    }
    if (days > 290 && days <= 319) {
        return  @"10Mth";
    }
    if (days > 319 && days <= 348) {
        return  @"11Mth";
    }
    if (days > 348 && days <= 360) {
        return  @"12Mth";
    }

    if (days > 360 && days <= 720) {
        return  @"1Yrs";
    }

    if (days > 720) {

        NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
        [formatter1 setDateFormat:@"MM/dd/yyyy"];
        NSString *fdisplay = [formatter1 stringFromDate:createdDate];
        return fdisplay;
    }

}

return result;
}


-(BOOL) todaysIsLess: (NSString *)dateToCompare{
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormatter1 setTimeZone:timeZone];
NSDate *today = [NSDate date];
NSDate *newDate = [dateFormatter1 dateFromString:dateToCompare];
NSComparisonResult result;
result = [today compare:newDate];
if(result==NSOrderedAscending)
    return true;
return false;
}

Example how to use it:

示例如何使用它:

 long createdDate = 1433183206;//1433183206 --> 01 June 2015
NSLog(@"Parse Date: %@",[self parseDate:createdDate]); 

#8


1  

//String to store the date from json response
   NSString *firstDateString;

 //Dateformatter as per the response date
NSDateFormatter *df=[[NSDateFormatter alloc] init];

// Set the date format according to your needs
[df setTimeZone:[NSTimeZone timeZoneWithName:@"America/Toronto"]];

//[df setDateFormat:@"MM/dd/YYYY HH:mm "]  // for 24 hour format
[df setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // 12 hour format


   firstDateString = value from json;    

 //converting the date to required format.
 NSDate *date1 = [df dateFromString:firstDateString];
  NSDate *date2 = [df dateFromString:[df stringFromDate:[NSDate date]]];  

    //Calculating the time interval
    NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];

    int numberOfDays = secondsBetween / 86400;
    int timeResult = ((int)secondsBetween % 86400);
    int hour = timeResult / 3600;
    int hourResult = ((int)timeResult % 3600);
    int minute = hourResult / 60;


    if(numberOfDays > 0)
    {
        if(numberOfDays == 1)
        {
            Nslog("%@", [NSString stringWithFormat:@"%d %@",numberOfDays,@"day ago"]);

        }
        else
        {
            Nslog("%@", [NSString stringWithFormat:@"%d %@",numberOfDays,@"days ago"]);
        }
    }
    else if(numberOfDays == 0 && hour > 0)
    {
        if(numberOfDays == 0 && hour == 1)
        {
            cell.newsDateLabel.text = [NSString stringWithFormat:@"%d %@",hour,@"hour ago"];
        }
        else
        {
            cell.newsDateLabel.text = [NSString stringWithFormat:@"%d %@",hour,NSLocalizedString(@"news_hours_ago",nil)];
        }
    }
    else if(numberOfDays == 0 && hour == 0 && minute > 0)
    {
        if(numberOfDays == 0 && hour == 0 && minute == 1)
        {
            cell.newsDateLabel.text = [NSString stringWithFormat:@"%d %@",minute,@"minute ago"];

        }
        else
        {
            cell.newsDateLabel.text = [NSString stringWithFormat:@"%d %@",minute,NSLocalizedString(@"news_minutes_ago",nil)];
        }

    }
    else
    {
        cell.newsDateLabel.text = [NSString stringWithFormat:NSLocalizedString(@"news_seconds_ago",nil)];
    }

#9


0  

        NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"MM"];
    NSDate *todayDate = [NSDate date];
    NSDate *yourJSONDate;
    if ([[dateFormat stringFromDate:todayDate] integerValue]==[[dateFormat stringFromDate:yourJSONDate] integerValue]) {
        //month is same
        [dateFormat setDateFormat:@"dd"];
        if ([[dateFormat stringFromDate:todayDate] integerValue]==[[dateFormat stringFromDate:yourJSONDate] integerValue]) {
        //date is same

        }
        else{
            //date differ
            // now here you can check value for date

        }

    }
    else{
        //month differ
        // now here you can check value for month
    }

Try this. Once you get same or differ, you can again check for values and make strings. In if loop you can embed other answers and can make more specific according to your requirements.

尝试这个。一旦相同或不同,您可以再次检查值并创建字符串。在if循环中,您可以嵌入其他答案,并可以根据您的要求更具体。

#10


0  

Try the following code:

请尝试以下代码:

+ (NSString*) getTimestampForDate:(NSDate*)sourceDate {

    // Timezone Offset compensation

    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:@"America/New_York"];
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];

    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];

    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

    NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];

    // Timestamp calculation (based on correction)

    NSCalendar* currentCalendar = [NSCalendar currentCalendar];
    NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;

    NSDateComponents *differenceComponents = [currentCalendar components:unitFlags fromDate:destinationDate toDate:[NSDate date] options:0];

    NSInteger yearDifference = [differenceComponents year];
    NSInteger monthDifference = [differenceComponents month];
    NSInteger dayDifference = [differenceComponents day];
    NSInteger hourDifference = [differenceComponents hour];
    NSInteger minuteDifference = [differenceComponents minute];

    NSString* timestamp;

    if (yearDifference == 0
        && monthDifference == 0
        && dayDifference == 0
        && hourDifference == 0
        && minuteDifference <= 2) {

        //"Just Now"

        timestamp = @"Just Now";

    } else if (yearDifference == 0
               && monthDifference == 0
               && dayDifference == 0
               && hourDifference == 0
               && minuteDifference < 60) {

        //"13 minutes ago"

        timestamp = [NSString stringWithFormat:@"%ld minutes ago", (long)minuteDifference];

    } else if (yearDifference == 0
               && monthDifference == 0
               && dayDifference == 0
               && hourDifference == 1) {

        //"1 hour ago" EXACT

        timestamp = @"1 hour ago";

    } else if (yearDifference == 0
               && monthDifference == 0
               && dayDifference == 0
               && hourDifference < 24) {

        timestamp = [NSString stringWithFormat:@"%ld hours ago", (long)hourDifference];

    } else {

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setLocale:[NSLocale currentLocale]];

        NSString* strDate, *strDate2 = @"";

        if (yearDifference == 0
            && monthDifference == 0
            && dayDifference == 1) {

            //"Yesterday at 10:23 AM", "Yesterday at 5:08 PM"

            [formatter setDateFormat:@"hh:mm a"];
            strDate = [formatter stringFromDate:destinationDate];

            timestamp = [NSString stringWithFormat:@"Yesterday at %@", strDate];

        } else if (yearDifference == 0
                   && monthDifference == 0
                   && dayDifference < 7) {

            //"Tuesday at 7:13 PM"

            [formatter setDateFormat:@"EEEE"];
            strDate = [formatter stringFromDate:destinationDate];
            [formatter setDateFormat:@"hh:mm a"];
            strDate2 = [formatter stringFromDate:destinationDate];

            timestamp = [NSString stringWithFormat:@"%@ at %@", strDate, strDate2];

        } else if (yearDifference == 0) {

            //"July 4 at 7:36 AM"

            [formatter setDateFormat:@"MMMM d"];
            strDate = [formatter stringFromDate:destinationDate];
            [formatter setDateFormat:@"hh:mm a"];
            strDate2 = [formatter stringFromDate:destinationDate];

            timestamp = [NSString stringWithFormat:@"%@ at %@", strDate, strDate2];

        } else {

            //"March 24 2010 at 4:50 AM"

            [formatter setDateFormat:@"d MMMM yyyy"];
            strDate = [formatter stringFromDate:destinationDate];
            [formatter setDateFormat:@"hh:mm a"];
            strDate2 = [formatter stringFromDate:destinationDate];

            timestamp = [NSString stringWithFormat:@"%@ at %@", strDate, strDate2];
        }
    }

    return timestamp;
}

NOTE: The first few lines are there for timezone offset correction. If it is not needed, comment it out, and use the sourceDate wherever destinationDate is used.

注意:前几行用于时区偏移校正。如果不需要,请将其注释掉,并在使用destinationDate的任何地方使用sourceDate。

#11


0  

Whatsapp conversation list kind of date formating....

Whatsapp会话列表日期格式化....

-(NSString *)getChatListFormatDate{

    NSString *differencDate = @"";

    NSDate *lastSeenDate = self;
    NSDate *currentDate = [NSDate date];

    NSString *timeDateStr = [self getStringFromDateFormat:@"hh:mm a"];
    NSString *dayOfWeekString = [lastSeenDate getStringFromDateFormat:@"EEEE"];

    NSCalendarUnit units = NSCalendarUnitDay | NSCalendarUnitWeekOfMonth | NSCalendarUnitWeekOfYear | NSCalendarUnitMonth | NSCalendarUnitYear;
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSDateComponents *calendarLastSeen = [calendar components:units fromDate:lastSeenDate];
    NSDateComponents *calendarToday = [calendar components:units fromDate:currentDate];

    BOOL isThisYear = calendarLastSeen.year == calendarToday.year;
    BOOL isThisMonth = calendarLastSeen.month == calendarToday.month;
    BOOL isThisWeekOfMonth = calendarLastSeen.weekOfMonth == calendarToday.weekOfMonth;

    NSInteger dayDiff = calendarToday.day - calendarLastSeen.day;

    if (isThisYear && isThisMonth && dayDiff == 0) {
        differencDate = timeDateStr;//Today
    }
    else if (isThisYear && isThisMonth && dayDiff == 1){
        differencDate = @"Yesterday";
    }
    else if (isThisYear && isThisMonth && isThisWeekOfMonth){
        differencDate = dayOfWeekString;//apply Date
    }
    else {
        NSString *strDate = [self getStringFromDateFormat:@"d/MM/yy"];
        differencDate = strDate;
    }
    return differencDate;
}

#1


16  

As of iOS 8, you can get a lot of help (including localization) by using NSDateComponentsFormatter. Note: you can easily change how much granularity the string shows by changing the allowedUnits on the components formatter if you want to return a string like "2 days, 3 hours, 5 minutes ago".

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

Swift 4:

斯威夫特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:

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]];
}

#2


7  

I used DateTools to achieve it. It supports Cocoapods installation.

我使用DateTools来实现它。它支持Cocoapods安装。

Works like so..

像这样工作..

NSDate *timeAgoDate = [NSDate dateWithTimeIntervalSinceNow:-4];
NSLog(@"Time Ago: %@", timeAgoDate.timeAgoSinceNow);
NSLog(@"Time Ago: %@", timeAgoDate.shortTimeAgoSinceNow);

//Output:
//Time Ago: 4 seconds ago
//Time Ago: 4s

taken from the Github page (https://github.com/MatthewYork/DateTools)

取自Github页面(https://github.com/MatthewYork/DateTools)

#3


6  

This function will return NSString starting from sec to years. Like if your date is of "1 sec ago" or if it is of "1 min ago" or "1 year ago" and so on.. it will return likewise..

此函数将从秒到年返回NSString。就像你的约会时间是“1秒前”,或者它是“1分钟前”或“1年前”等等......它会同样返回...

+(NSString*)HourCalculation:(NSString*)PostDate

{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    [dateFormat setTimeZone:gmt];
    NSDate *ExpDate = [dateFormat dateFromString:PostDate];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSDayCalendarUnit|NSWeekCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:ExpDate toDate:[NSDate date] options:0];
    NSString *time;
    if(components.year!=0)
    {
        if(components.year==1)
        {
            time=[NSString stringWithFormat:@"%ld year",(long)components.year];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld years",(long)components.year];
        }
    }
    else if(components.month!=0)
    {
        if(components.month==1)
        {
            time=[NSString stringWithFormat:@"%ld month",(long)components.month];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld months",(long)components.month];
        }
    }
    else if(components.week!=0)
    {
        if(components.week==1)
        {
            time=[NSString stringWithFormat:@"%ld week",(long)components.week];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld weeks",(long)components.week];
        }
    }
    else if(components.day!=0)
    {
        if(components.day==1)
        {
            time=[NSString stringWithFormat:@"%ld day",(long)components.day];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld days",(long)components.day];
        }
    }
    else if(components.hour!=0)
    {
        if(components.hour==1)
        {
            time=[NSString stringWithFormat:@"%ld hour",(long)components.hour];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld hours",(long)components.hour];
        }
    }
    else if(components.minute!=0)
    {
        if(components.minute==1)
        {
            time=[NSString stringWithFormat:@"%ld min",(long)components.minute];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld mins",(long)components.minute];
        }
    }
    else if(components.second>=0)
    {
        if(components.second==0)
        {
            time=[NSString stringWithFormat:@"1 sec"];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld secs",(long)components.second];
        }
    }
    return [NSString stringWithFormat:@"%@ ago",time];
}

#4


5  

Simplified implementation of @jDutton

简化了@jDutton的实现

swift4, swift3

swift4,swift3

extension Date {
   var timestampString: String? {
      let formatter = DateComponentsFormatter()
      formatter.unitsStyle = .full
      formatter.maximumUnitCount = 1
      formatter.allowedUnits = [.year, .month, .day, .hour, .minute, .second]

      guard let timeString = formatter.string(from: self, to: Date()) else {
           return nil
      }

      let formatString = NSLocalizedString("%@ ago", comment: "")
      return String(format: formatString, timeString)
   }
}

swift2

swift2

extension NSDate {
   var timestampString: String? {
      let formatter = NSDateComponentsFormatter()
      formatter.unitsStyle = .Full
      formatter.maximumUnitCount = 1
      formatter.allowedUnits = [.Year, .Month, .Day, .Hour, .Minute, .Second]

      guard let timeString = formatter.stringFromDate(self, toDate: NSDate()) else {
         return nil
      }

      let formatString = NSLocalizedString("%@ ago", comment: "")
      return String(format: formatString, timeString)
   }
}

#5


4  

One option is you can compare the current time and previous one and implement switch cases to get the string that you want.

一个选项是您可以比较当前时间和前一个时间并实现切换案例以获取所需的字符串。

Or you can use any of the following libraries:

或者您可以使用以下任何库:

  1. FormatterKit
  2. FormatterKit
  3. NSDate-Time-Ago
  4. NSDate的时间同期

I've created a library for swift, you can get it from here : Past

我已经为swift创建了一个库,你可以从这里得到它:过去

#6


4  

Swift 3, Xcode version 8.2.1:

Swift 3,Xcode版本8.2.1:

func elapsedTime () -> String
{
    //just to create a date that is before the current time
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    dateFormatter.locale = Locale.init(identifier: "en_GB")
    let before = dateFormatter.date(from: "2016-11-16 16:28:17")!

    //getting the current time
    let now = Date()

    let formatter = DateComponentsFormatter()
    formatter.unitsStyle = .full
    formatter.zeroFormattingBehavior = .dropAll
    formatter.maximumUnitCount = 1 //increase it if you want more precision
    formatter.allowedUnits = [.year, .month, .weekOfMonth, .day, .hour, .minute]
    formatter.includesApproximationPhrase = true //to write "About" at the beginning


    let formatString = NSLocalizedString("%@ ago", comment: "Used to say how much time has passed. e.g. '2 hours ago'")
    let timeString = formatter.string(from: before, to: now)
    return String(format: formatString, timeString!)
}

#7


4  

You can find also an example here: https://github.com/tneginareb/Time-Ago-iOS

你可以在这里找到一个例子:https://github.com/tneginareb/Time-Ago-iOS

You can modify the input variable "timeAtMiliseconds", for my example was at the format of date was at Milliseconds.

您可以修改输入变量“timeAtMiliseconds”,因为我的示例是日期格式为毫秒。

+(NSString *) parseDate: (long)dayago{

if(dayago == 0)
    return @"";

NSString *timeLength =[NSString stringWithFormat:@"%lu",dayago];
NSUInteger length = [timeLength length];
if(length == 13){
    dayago = dayago / 1000;

}

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *createdDate = [NSDate dateWithTimeIntervalSince1970:dayago];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
[dateFormatter setTimeZone:timeZone];
NSString *formattedDateString = [dateFormatter stringFromDate:createdDate];

if([self todaysIsLess:formattedDateString]){
    return @"";
}


NSString *timeLeft;
NSDate *currentDate =[NSDate date];
NSInteger seconds = [currentDate timeIntervalSinceDate:createdDate];




NSInteger days = (int) (floor(seconds / (3600 * 24)));
if(days) seconds -= days * 3600 * 24;

NSInteger hours = (int) (floor(seconds / 3600));
if(hours) seconds -= hours * 3600;

NSInteger minutes = (int) (floor(seconds / 60));
if(minutes) seconds -= minutes * 60;

if(days) {
    timeLeft = [NSString stringWithFormat:@"%ld Days", (long)days*-1];
}
else if(hours) { timeLeft = [NSString stringWithFormat: @"%ld H", (long)hours*-1];
}
else if(minutes) { timeLeft = [NSString stringWithFormat: @"%ld M", (long)minutes*-1];
}
else if(seconds)
    timeLeft = [NSString stringWithFormat: @"%lds", (long)seconds*-1];
//NSLog(@"Days: %lu <>  Hours: %lu <> Minutes: %lu  <> Seconds: %lu",days,hours,minutes,seconds);

NSString *result = [[NSString alloc]init];

if (days == 0) {
    if (hours == 0) {
        if (minutes == 0) {
            if (seconds < 0) {
                return @"0s";
            } else {
                if (seconds < 59) {
                    return @"now";
                }
            }
        } else {
            return  [NSString stringWithFormat:@"%lum",minutes];
        }
    } else {
        return  [NSString stringWithFormat:@"%luh",hours];
    }

} else {
    if (days <= 29) {
        return  [NSString stringWithFormat: @"%lud",days];
    }
    if (days > 29 && days <= 58) {
        return  @"1Mth";
    }
    if (days > 58 && days <= 87) {
        return  @"2Mth";
    }
    if (days > 87 && days <= 116) {
        return  @"3Mth";
    }
    if (days > 116 && days <= 145) {
        return  @"4Mth";
    }
    if (days > 145 && days <= 174) {
        return  @"5Mth";
    }
    if (days > 174 && days <= 203) {
        return  @"6Mth";
    }
    if (days > 203 && days <= 232) {
        return  @"7Mth";
    }
    if (days > 232 && days <= 261) {
        return  @"8Mth";
    }
    if (days > 261 && days <= 290) {
        return  @"9Mth";
    }
    if (days > 290 && days <= 319) {
        return  @"10Mth";
    }
    if (days > 319 && days <= 348) {
        return  @"11Mth";
    }
    if (days > 348 && days <= 360) {
        return  @"12Mth";
    }

    if (days > 360 && days <= 720) {
        return  @"1Yrs";
    }

    if (days > 720) {

        NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
        [formatter1 setDateFormat:@"MM/dd/yyyy"];
        NSString *fdisplay = [formatter1 stringFromDate:createdDate];
        return fdisplay;
    }

}

return result;
}


-(BOOL) todaysIsLess: (NSString *)dateToCompare{
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormatter1 setTimeZone:timeZone];
NSDate *today = [NSDate date];
NSDate *newDate = [dateFormatter1 dateFromString:dateToCompare];
NSComparisonResult result;
result = [today compare:newDate];
if(result==NSOrderedAscending)
    return true;
return false;
}

Example how to use it:

示例如何使用它:

 long createdDate = 1433183206;//1433183206 --> 01 June 2015
NSLog(@"Parse Date: %@",[self parseDate:createdDate]); 

#8


1  

//String to store the date from json response
   NSString *firstDateString;

 //Dateformatter as per the response date
NSDateFormatter *df=[[NSDateFormatter alloc] init];

// Set the date format according to your needs
[df setTimeZone:[NSTimeZone timeZoneWithName:@"America/Toronto"]];

//[df setDateFormat:@"MM/dd/YYYY HH:mm "]  // for 24 hour format
[df setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // 12 hour format


   firstDateString = value from json;    

 //converting the date to required format.
 NSDate *date1 = [df dateFromString:firstDateString];
  NSDate *date2 = [df dateFromString:[df stringFromDate:[NSDate date]]];  

    //Calculating the time interval
    NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];

    int numberOfDays = secondsBetween / 86400;
    int timeResult = ((int)secondsBetween % 86400);
    int hour = timeResult / 3600;
    int hourResult = ((int)timeResult % 3600);
    int minute = hourResult / 60;


    if(numberOfDays > 0)
    {
        if(numberOfDays == 1)
        {
            Nslog("%@", [NSString stringWithFormat:@"%d %@",numberOfDays,@"day ago"]);

        }
        else
        {
            Nslog("%@", [NSString stringWithFormat:@"%d %@",numberOfDays,@"days ago"]);
        }
    }
    else if(numberOfDays == 0 && hour > 0)
    {
        if(numberOfDays == 0 && hour == 1)
        {
            cell.newsDateLabel.text = [NSString stringWithFormat:@"%d %@",hour,@"hour ago"];
        }
        else
        {
            cell.newsDateLabel.text = [NSString stringWithFormat:@"%d %@",hour,NSLocalizedString(@"news_hours_ago",nil)];
        }
    }
    else if(numberOfDays == 0 && hour == 0 && minute > 0)
    {
        if(numberOfDays == 0 && hour == 0 && minute == 1)
        {
            cell.newsDateLabel.text = [NSString stringWithFormat:@"%d %@",minute,@"minute ago"];

        }
        else
        {
            cell.newsDateLabel.text = [NSString stringWithFormat:@"%d %@",minute,NSLocalizedString(@"news_minutes_ago",nil)];
        }

    }
    else
    {
        cell.newsDateLabel.text = [NSString stringWithFormat:NSLocalizedString(@"news_seconds_ago",nil)];
    }

#9


0  

        NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"MM"];
    NSDate *todayDate = [NSDate date];
    NSDate *yourJSONDate;
    if ([[dateFormat stringFromDate:todayDate] integerValue]==[[dateFormat stringFromDate:yourJSONDate] integerValue]) {
        //month is same
        [dateFormat setDateFormat:@"dd"];
        if ([[dateFormat stringFromDate:todayDate] integerValue]==[[dateFormat stringFromDate:yourJSONDate] integerValue]) {
        //date is same

        }
        else{
            //date differ
            // now here you can check value for date

        }

    }
    else{
        //month differ
        // now here you can check value for month
    }

Try this. Once you get same or differ, you can again check for values and make strings. In if loop you can embed other answers and can make more specific according to your requirements.

尝试这个。一旦相同或不同,您可以再次检查值并创建字符串。在if循环中,您可以嵌入其他答案,并可以根据您的要求更具体。

#10


0  

Try the following code:

请尝试以下代码:

+ (NSString*) getTimestampForDate:(NSDate*)sourceDate {

    // Timezone Offset compensation

    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:@"America/New_York"];
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];

    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];

    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

    NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];

    // Timestamp calculation (based on correction)

    NSCalendar* currentCalendar = [NSCalendar currentCalendar];
    NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;

    NSDateComponents *differenceComponents = [currentCalendar components:unitFlags fromDate:destinationDate toDate:[NSDate date] options:0];

    NSInteger yearDifference = [differenceComponents year];
    NSInteger monthDifference = [differenceComponents month];
    NSInteger dayDifference = [differenceComponents day];
    NSInteger hourDifference = [differenceComponents hour];
    NSInteger minuteDifference = [differenceComponents minute];

    NSString* timestamp;

    if (yearDifference == 0
        && monthDifference == 0
        && dayDifference == 0
        && hourDifference == 0
        && minuteDifference <= 2) {

        //"Just Now"

        timestamp = @"Just Now";

    } else if (yearDifference == 0
               && monthDifference == 0
               && dayDifference == 0
               && hourDifference == 0
               && minuteDifference < 60) {

        //"13 minutes ago"

        timestamp = [NSString stringWithFormat:@"%ld minutes ago", (long)minuteDifference];

    } else if (yearDifference == 0
               && monthDifference == 0
               && dayDifference == 0
               && hourDifference == 1) {

        //"1 hour ago" EXACT

        timestamp = @"1 hour ago";

    } else if (yearDifference == 0
               && monthDifference == 0
               && dayDifference == 0
               && hourDifference < 24) {

        timestamp = [NSString stringWithFormat:@"%ld hours ago", (long)hourDifference];

    } else {

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setLocale:[NSLocale currentLocale]];

        NSString* strDate, *strDate2 = @"";

        if (yearDifference == 0
            && monthDifference == 0
            && dayDifference == 1) {

            //"Yesterday at 10:23 AM", "Yesterday at 5:08 PM"

            [formatter setDateFormat:@"hh:mm a"];
            strDate = [formatter stringFromDate:destinationDate];

            timestamp = [NSString stringWithFormat:@"Yesterday at %@", strDate];

        } else if (yearDifference == 0
                   && monthDifference == 0
                   && dayDifference < 7) {

            //"Tuesday at 7:13 PM"

            [formatter setDateFormat:@"EEEE"];
            strDate = [formatter stringFromDate:destinationDate];
            [formatter setDateFormat:@"hh:mm a"];
            strDate2 = [formatter stringFromDate:destinationDate];

            timestamp = [NSString stringWithFormat:@"%@ at %@", strDate, strDate2];

        } else if (yearDifference == 0) {

            //"July 4 at 7:36 AM"

            [formatter setDateFormat:@"MMMM d"];
            strDate = [formatter stringFromDate:destinationDate];
            [formatter setDateFormat:@"hh:mm a"];
            strDate2 = [formatter stringFromDate:destinationDate];

            timestamp = [NSString stringWithFormat:@"%@ at %@", strDate, strDate2];

        } else {

            //"March 24 2010 at 4:50 AM"

            [formatter setDateFormat:@"d MMMM yyyy"];
            strDate = [formatter stringFromDate:destinationDate];
            [formatter setDateFormat:@"hh:mm a"];
            strDate2 = [formatter stringFromDate:destinationDate];

            timestamp = [NSString stringWithFormat:@"%@ at %@", strDate, strDate2];
        }
    }

    return timestamp;
}

NOTE: The first few lines are there for timezone offset correction. If it is not needed, comment it out, and use the sourceDate wherever destinationDate is used.

注意:前几行用于时区偏移校正。如果不需要,请将其注释掉,并在使用destinationDate的任何地方使用sourceDate。

#11


0  

Whatsapp conversation list kind of date formating....

Whatsapp会话列表日期格式化....

-(NSString *)getChatListFormatDate{

    NSString *differencDate = @"";

    NSDate *lastSeenDate = self;
    NSDate *currentDate = [NSDate date];

    NSString *timeDateStr = [self getStringFromDateFormat:@"hh:mm a"];
    NSString *dayOfWeekString = [lastSeenDate getStringFromDateFormat:@"EEEE"];

    NSCalendarUnit units = NSCalendarUnitDay | NSCalendarUnitWeekOfMonth | NSCalendarUnitWeekOfYear | NSCalendarUnitMonth | NSCalendarUnitYear;
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSDateComponents *calendarLastSeen = [calendar components:units fromDate:lastSeenDate];
    NSDateComponents *calendarToday = [calendar components:units fromDate:currentDate];

    BOOL isThisYear = calendarLastSeen.year == calendarToday.year;
    BOOL isThisMonth = calendarLastSeen.month == calendarToday.month;
    BOOL isThisWeekOfMonth = calendarLastSeen.weekOfMonth == calendarToday.weekOfMonth;

    NSInteger dayDiff = calendarToday.day - calendarLastSeen.day;

    if (isThisYear && isThisMonth && dayDiff == 0) {
        differencDate = timeDateStr;//Today
    }
    else if (isThisYear && isThisMonth && dayDiff == 1){
        differencDate = @"Yesterday";
    }
    else if (isThisYear && isThisMonth && isThisWeekOfMonth){
        differencDate = dayOfWeekString;//apply Date
    }
    else {
        NSString *strDate = [self getStringFromDateFormat:@"d/MM/yy"];
        differencDate = strDate;
    }
    return differencDate;
}