Aarti Oza
Aarti Oza

Reputation: 1154

Date and Time Format in iOS application

I am getting below date format from response

2015-12-23 05:17:04

But, I want to check date and time like,

date == today ? date == yesterday ?

time == 1 hour time == 2 hour till 9 hour

how to do it ?

Upvotes: 0

Views: 1312

Answers (4)

Mehul Sojitra
Mehul Sojitra

Reputation: 1181

To check whether date is today's, yesterday's, tomorrow's date etc... you can use NSDate-Extensions

ex:

NSDate *date = [self convertStringToDate:@"2015-12-24 12:40:04" formate:@"yyyy-MM-dd HH:mm:ss"];
NSLog(@"%d", [date isToday]);
NSLog(@"%d", [date isTomorrow]);
NSLog(@"%d", [date isYesterday]);

- (NSDate *)convertStringToDate: (NSString *)date formate:(NSString *)formate {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    [dateFormat setDateFormat:formate];
    return [dateFormat dateFromString:date];
}

Upvotes: 2

Hai Lu
Hai Lu

Reputation: 11

1.frist you can format your string to date,like this:

+ (NSDate *)dateFromString:(NSString *)dateString {
if (!dateString) {
    return nil;
}

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
[formatter setLocale:[NSLocale currentLocale]];
NSDate *date= [formatter dateFromString:dateString];

return date;
}

2.then,you can use methods of NSDate,

+ (instancetype)date;//get current
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)refDate;//Returns the interval between the receiver and another given date.

Upvotes: 1

tubiebutu
tubiebutu

Reputation: 11

NSDate *date = [NSDate date];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

dateFormatter.dateFormat = [NSString stringWithFormat:@"yyyyMMddHHmmss"];

NSString *now = [dateFormatter stringFromDate:date];

You can try above code

Best wish

Upvotes: 1

Avijit Nagare
Avijit Nagare

Reputation: 8782

Use this method give you NSString back. you can compare string or time in method.

-(NSString* )AgoStringFromTime:(NSDate*)dateTime
{
    NSDictionary *timeScale = @{@"sec"  :@1,
                                @"min"  :@60,
                                @"hr"   :@3600,
                                @"day"  :@86400,
                                @"week" :@605800,
                                @"month":@2629743,
                                @"year" :@31556926};
    NSString *scale;
    int timeAgo = 0-(int)[dateTime timeIntervalSinceNow];

    if (timeAgo < 60) {
        scale = @"sec";
    } else if (timeAgo < 3600) {
        scale = @"min";
    } else if (timeAgo < 86400) {
        scale = @"hr";
    } else if (timeAgo < 605800) {
        scale = @"day";
    } else if (timeAgo < 2629743) {
        scale = @"week";
    } else if (timeAgo < 31556926) {
        scale = @"month";
    } else {
        scale = @"year";
    }

    timeAgo = timeAgo/[[timeScale objectForKey:scale] integerValue];
    NSString *s = @"";
    if (timeAgo > 1) {
        s = @"s";
    }

    return [NSString stringWithFormat:@"%d %@%@", timeAgo, scale, s];
}

Upvotes: 1

Related Questions