Skip to content

Instantly share code, notes, and snippets.

@JeOam
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeOam/bab077275eb470bc3573 to your computer and use it in GitHub Desktop.
Save JeOam/bab077275eb470bc3573 to your computer and use it in GitHub Desktop.
How do I get the day of the week in Objective-C?
- (void)viewDidLoad
{
NSDate *currentDate = [NSDate date];
NSLog(@"Current Date: %@", currentDate);
// Outputs current day of week as a string in locale dependent on current regional settings.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE"];
NSLog(@"The day of the week: %@", [dateFormatter stringFromDate:[NSDate date]]);
// To get just a week day number you must use NSCalendar class
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
int weekday = [comps weekday];
NSLog(@"The week day number: %d", weekday);
}
@JeOam
Copy link
Author

JeOam commented Jun 5, 2014

via: here

@JeOam
Copy link
Author

JeOam commented Jun 5, 2014

NSDateFormatter 格式说明:

  • G: 公元时代,例如AD公元
  • yy: 年的后2位
  • yyyy: 完整年
  • MM: 月,显示为1-12
  • MMM: 月,显示为英文月份简写,如 Jan
  • MMMM: 月,显示为英文月份全称,如 Janualy
  • dd: 日,2位数表示,如02
  • d: 日,1-2位显示,如 2
  • EEE: 简写星期几,如Sun
  • EEEE: 全写星期几,如Sunday
  • aa: 上下午,AM/PM
  • H: 时,24小时制,0-23
  • K:时,12小时制,0-11
  • m: 分,1-2位
  • mm: 分,2位
  • s: 秒,1-2位
  • ss: 秒,2位
  • S: 毫秒

常用日期结构:

  • yyyy-MM-dd HH:mm:ss.SSS
  • yyyy-MM-dd HH:mm:ss
  • yyyy-MM-dd
  • MM dd yyyy

via: click

@JeOam
Copy link
Author

JeOam commented Jun 24, 2014

要想将 2014-06-04 00:00:00 的字符串中抽取 06-04 出来:

- (NSString *)translateDateString:(NSString *)dateString FromFormat:(NSString *)oldFormate toFormat:(NSString *)updateFormat{
    NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init];
    [dateFormater setDateFormat:oldFormate];
    NSDate *timeDate = [dateFormater dateFromString:dateString];
    [dateFormater setDateFormat:updateFormat];
    return [dateFormater stringFromDate:timeDate];
}

NSString *string =  [self translateDateString:dateString FromFormat:@"yyyy-MM-dd HH:mm:ss" toFormat:@"MM-dd"];

@JeOam
Copy link
Author

JeOam commented Jul 8, 2014

Convert NSDate in to relative format as “Today”,“Yesterday”:

#define SECOND  (1)
#define MINUTE  (60 * SECOND)
#define HOUR    (60 * MINUTE)
#define DAY     (24 * HOUR)
#define MONTH   (30 * DAY)
#define YEAR    (12 * MONTH)

- (NSString *)timeAgo:(NSdate *)date
{
    NSTimeInterval delta = [[NSDate date] timeIntervalSinceDate:date];

    if (delta < 1 * MINUTE)
    {
        return @"刚刚";
    }
    else if (delta < 2 * MINUTE)
    {
        return @"1分钟前";
    }
    else if (delta < 45 * MINUTE)
    {
        int minutes = floor((double)delta/MINUTE);
        return [NSString stringWithFormat:@"%d分钟前", minutes];
    }
    else if (delta < 90 * MINUTE)
    {
        return @"1小时前";
    }
    else if (delta < 24 * HOUR)
    {
        int hours = floor((double)delta/HOUR);
        return [NSString stringWithFormat:@"%d小时前", hours];
    }
    else if (delta < 48 * HOUR)
    {
        return @"昨天";
    }
    else if (delta < 30 * DAY)
    {
        int days = floor((double)delta/DAY);
        return [NSString stringWithFormat:@"%d天前", days];
    }
    else if (delta < 12 * MONTH)
    {
        int months = floor((double)delta/MONTH);
        return months <= 1 ? @"1个月前" : [NSString stringWithFormat:@"%d个月前", months];
    }

    int years = floor((double)delta/MONTH/12.0);
    return years <= 1 ? @"1年前" : [NSString stringWithFormat:@"%d年前", years];
}

via NSDate+BeeExtension.h

@JeOam
Copy link
Author

JeOam commented Jul 21, 2014

How can I calculate the difference between two dates?

NSDate *date1 = [NSDate dateWithString:@"2010-01-01 00:00:00 +0000"];
NSDate *date2 = [NSDate dateWithString:@"2010-02-03 00:00:00 +0000"];

NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];

int numberOfDays = secondsBetween / 86400;

NSLog(@"There are %d days in between the two dates.", numberOfDays);

via How can I calculate the difference between two dates?

@JeOam
Copy link
Author

JeOam commented Aug 7, 2014

Get local current time string:

NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *currentTime = [dateFormatter stringFromDate:today];

@JeOam
Copy link
Author

JeOam commented Apr 27, 2015

DateFormatter:

yyyy-MM-dd'T'HH:mm:ss.ZZZZ
2015-04-27T03:02:52+00:00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment