Last active
August 29, 2015 14:02
-
-
Save JeOam/bab077275eb470bc3573 to your computer and use it in GitHub Desktop.
How do I get the day of the week in Objective-C?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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); | |
} |
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
要想将 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"];
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
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);
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];
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
via: here