0

I'm trying to create a function which rearrange the order of a dateString. I'm having a array of news dates which i basically wants to check whether they are from today or yesterday. The dateString looks like following:

2015-03-23 18:58:00

So if they are today i want a ouput like this:

Today, 16:30

if they are from yesterday

Yesterday, 16:30

and if they are not today or yester day it should be something like

 1. April, 16:30

So far i've created this function which just has the currentDate and newsDate. How can i compare them to get the above logic?

func getDate(dateStr:String, format:String = "yyyy-MM-dd HH:mm:ss") -> String {

    //news DateTime
    var dateFmt = NSDateFormatter()
    dateFmt.timeZone = NSTimeZone.defaultTimeZone()
    dateFmt.dateFormat = format
    let newsDateTime = dateFmt.dateFromString(dateStr)!



    //currentDate
    let date = NSDate();
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    dateFormatter.timeZone = NSTimeZone()
    let localDate = dateFormatter.stringFromDate(date)


    //newsDate
    let newsDateFmt = NSDateFormatter()
    newsDateFmt.dateFormat = "yyyy-MM-dd"
    let newsDate = newsDateFmt.stringFromDate(newsDateTime)

    let toStringDate = NSDateFormatter()
    return toStringDate.stringFromDate(newsDateTime)
}
Peter Pik
  • 11,023
  • 19
  • 84
  • 142

3 Answers3

0

Check the individual Date components against the current DateTime.

Possible duplicate of this thread?

Community
  • 1
  • 1
jProg2015
  • 1,098
  • 10
  • 40
0

hey i m begging in swift so i just give you logic to do it.

  1. first compare date to system date.

    if (date is equal to system date)

       NSLog("Today");
    
  2. get day from date and compare it and check it's yesterday date or not

  3. else take month of that date.

see in one of my project i compare date.

NSArray *arrSortDate = [arrDate sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
        NSDate *d1 = [formatter dateFromString:obj1[@"key"]];
        NSDate *d2 = [formatter dateFromString:obj2[@"key"]];

        return [d2 compare:d1];
    }];

Here arrDate is NSMutableArray. It work.

Keyur Akbari
  • 182
  • 1
  • 13
0

I will answer in objective-C because i'm sure you can extract the swift out of it, here is a method that will react differently if the given date is earlier than today, today exactly, or a week from now, or month, or year(s).

+ (NSString*)cellDateFormatWithDate:(NSDate*)date{
NSCalendarUnit units = NSDayCalendarUnit | NSWeekOfYearCalendarUnit |
    NSMonthCalendarUnit | NSYearCalendarUnit;

    // if `date` is before "now" (i.e. in the past) then the components will be positive
    NSDateComponents *components = [[NSCalendar currentCalendar] components:units
                                                                   fromDate:date
                                                                     toDate:[NSDate date]
                                                                    options:0];

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    if (components.year > 0) {
        [formatter setDateFormat:@"dd/MM/yy"];
    } else if (components.month > 0) {
        [formatter setDateFormat:@"dd/MM/yy"];
    } else if (components.weekOfYear > 0) {
        [formatter setDateFormat:@"dd/MM HH:mm"];
    } else if (components.day > 0) {
        if (components.day > 1) {
            [formatter setDateFormat:@"dd/MM HH:mm"];
        } else {
            [formatter setDateFormat:@"dd/MM HH:mm"];
        }
    } else {
        [formatter setDateFormat:@"HH:mm"];
    }

    NSString *cellDate = [formatter stringFromDate:date];

    return cellDate;
}

To fit your question, you just need to add an

NSString* relativeString;

and in each if/else, change it accordingly

relativeString = @"Today";
relativeString = @"Yesterday";
relativeString = @"A week ago"; 

and so on.

This other method does pretty much the same but with second/minute accuracy

+ (NSString *)dateDiff:(NSString *)origDate{
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setFormatterBehavior:NSDateFormatterBehavior10_4];

    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
    NSDate *convertedDate = [df dateFromString:origDate];

    NSDate *todayDate = [NSDate date];
    double ti = [convertedDate timeIntervalSinceDate:todayDate];
    ti = ti * -1;

    if(ti < 1) {
        return NSLocalizedString(@"REL_TIME_NOW", nil);
    } else  if (ti < 60) {
        return NSLocalizedString(@"REL_TIME_LESS_THAN_MINUTE", nil);
    } else if (ti < 3600) {
        int diff = round(ti / 60);
        if (diff < 2){
            return [NSString stringWithFormat:NSLocalizedString(@"REL_TIME_MINUTE", nil), diff];
        }else{
          return [NSString stringWithFormat:NSLocalizedString(@"REL_TIME_MINUTES", nil), diff];
        }
    } else if (ti < 86400) {
        int diff = round(ti / 60 / 60);
        if (diff < 2){
            return [NSString stringWithFormat:NSLocalizedString(@"REL_TIME_HOUR", nil), diff];
        }else{
            return [NSString stringWithFormat:NSLocalizedString(@"REL_TIME_HOURS", nil), diff];
        }
    } else  {
        int diff = round(ti / 60 / 60 / 24);
        if (diff < 2){
            return [NSString stringWithFormat:NSLocalizedString(@"REL_TIME_DAY", nil), diff];
        }else{
            return [NSString stringWithFormat:NSLocalizedString(@"REL_TIME_DAYS", nil), diff];
        }
    }
}

Though I can't be bothered renaming all my localized strings, they just show

"less than a minute ago"
"%d minute ago"
"%d minutes ago"
"%d hour ago

" and so on

Gil Sand
  • 5,802
  • 5
  • 36
  • 78