我有一个NSDate对象,需要一天中的整数。即如果我们在2010年5月25日,则int应为25.是否有一种简单的方法可以做到这一点?
答案 0 :(得分:17)
请考虑this post如何从NSDate
获取日历组件。基本上它看起来像:
NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents* components = [calendar components:NSDayCalendarUnit
fromDate:myDate];
NSInteger day = [components day];
(不要忘记上面的内存管理。)
答案 1 :(得分:5)
如果您只需要NSDate的“25”部分,您可以从dateFormatter获取它。
类似的东西:
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd"];
NSString *dayInMonthStr = [dateFormatter stringFromDate:today];
int dayInMonth = [dayInMonthStr intValue];
[dateFormatter release];
NSLog(@"Today is the %i. day of the month", dayInMonth);
答案 2 :(得分:3)
请注意,NSDate只是一个时间戳,只有在考虑给定日历和时区时才有“天”。如果您想要当前时区的公历,
NSTimeZone * tz = [NSTimeZone localTimeZone];
CFAbsoluteTime at = CFDateGetAbsoluteTime((CFDateRef)date);
int day = CFAbsoluteTimeGetGregorianDate(at, (CFTimeZoneRef)tz).day;
如果您想要UTC日,请设置tz = nil
。
此外,CFAbsoluteTime和NSDate是(据我所知)基于POSIX时间指定86400秒的日期,因此不处理闰秒。
答案 3 :(得分:2)
使用“EEEE”作为当天全名的格式。
使用“EEE”作为当天短名称的格式。
示例:
- (NSString *)getDayOfTheWeek:(NSDate *)date{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init] ;
[dateFormatter setDateFormat:@"EEEE"];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
return formattedDateString;
}
答案 4 :(得分:-1)
我有一段时间没遇到这个问题了,我创建了以下方法来使一切变得更容易。
<强>解释强>
#define DATE_TYPE_hhmmss [NSArray arrayWithObjects:@"h", @"m", @"s", nil]
#define DATE_TYPE_MMDDYYYY [NSArray arrayWithObjects:@"M", @"D", @"Y", nil]
#define DATE_TYPE_MMDDYYYYhhmmss [NSArray arrayWithObjects:@"M", @"D", @"Y", @"h", @"m", @"s", nil]
#define DATE_TYPE_MMDDYYYYWWhhmmss [NSArray arrayWithObjects:@"M", @"D", @"Y", @"W", @"h", @"m", @"s", nil]
#define DATE_TYPE_MMDDYYYYhhmmssWW [NSArray arrayWithObjects:@"M", @"D", @"Y", @"h", @"m", @"s", @"W", nil]
#define DATE_TYPE_YYYYMMDD [NSArray arrayWithObjects:@"Y", @"M", @"D", nil]
#define DATE_TYPE_YYYYMMDDhhmmss [NSArray arrayWithObjects:@"Y", @"M", @"D", @"h", @"m", @"s", nil]
#define DATE_TYPE_YYYYMMDDWWhhmmss [NSArray arrayWithObjects:@"Y", @"M", @"D", @"W", @"h", @"m", @"s", nil]
#define DATE_TYPE_YYYYMMDDhhmmssWW [NSArray arrayWithObjects:@"Y", @"M", @"D", @"h", @"m", @"s", @"W", nil]
#define DATE_TYPE_FRIENDLY [NSArray arrayWithObjects:@"xx", nil]
日期方法
从值创建日期
-(NSDate *) getDateWithMonth:(int)month day:(int)day year:(int)year {
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
NSDateComponents * dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]];
[dateFormatter setDateFormat:@"MM"];
[dateComponents setMonth:month];
[dateFormatter setDateFormat:@"DD"];
[dateComponents setDay:day];
[dateFormatter setDateFormat:@"YYYY"];
[dateComponents setYear:year];
NSDate * result = [calendar dateFromComponents:dateComponents];
return result;
}
-(NSDate *) getDateWithMonth:(int)month day:(int)day year:(int)year hour:(int)hour minute:(int)minute {
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
NSDateComponents * dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]];
[dateFormatter setDateFormat:@"MM"];
[dateComponents setMonth:month];
[dateFormatter setDateFormat:@"DD"];
[dateComponents setDay:day];
[dateFormatter setDateFormat:@"YYYY"];
[dateComponents setYear:year];
[dateFormatter setDateFormat:@"HH"];
[dateComponents setHour:hour];
[dateFormatter setDateFormat:@"MM"];
[dateComponents setMinute:minute];
NSDate * result = [calendar dateFromComponents:dateComponents];
return result;
}
-(NSDate *) getDateWithMonth:(int)month day:(int)day year:(int)year hour:(int)hour minute:(int)minute second:(int)second {
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
NSDateComponents * dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[NSDate date]];
[dateFormatter setDateFormat:@"MM"];
[dateComponents setMonth:month];
[dateFormatter setDateFormat:@"DD"];
[dateComponents setDay:day];
[dateFormatter setDateFormat:@"YYYY"];
[dateComponents setYear:year];
[dateFormatter setDateFormat:@"HH"];
[dateComponents setHour:hour];
[dateFormatter setDateFormat:@"MM"];
[dateComponents setMinute:minute];
[dateFormatter setDateFormat:@"SS"];
[dateComponents setSecond:second];
NSDate * result = [calendar dateFromComponents:dateComponents];
return result;
}
从日期获取字符串
-(NSString *) getStringFromDate:(NSDate *)date dateType:(NSArray *)dateType {
NSString * result = @"";
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSString * format = @"";
NSDateComponents * dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate:date];
NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
NSInteger day = [dateComponents day];
NSInteger weekday = [dateComponents weekday];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];
if (dateType != nil) {
for (int x = 0; x < [dateType count]; x++) {
if (x == ([dateType count]-1)) {
if ([[dateType objectAtIndex:x] isEqualToString:@"Y"]) {
format = [format stringByAppendingFormat:@"%d", year];
} else if ([[dateType objectAtIndex:x] isEqualToString:@"M"]) {
format = [format stringByAppendingFormat:@"%d", month];
} else if ([[dateType objectAtIndex:x] isEqualToString:@"D"]) {
format = [format stringByAppendingFormat:@"%d", day];
} else if ([[dateType objectAtIndex:x] isEqualToString:@"W"]) {
format = [format stringByAppendingFormat:@"%d", weekday];
} else if ([[dateType objectAtIndex:x] isEqualToString:@"h"]) {
format = [format stringByAppendingFormat:@"%d", hour];
} else if ([[dateType objectAtIndex:x] isEqualToString:@"m"]) {
format = [format stringByAppendingFormat:@"%d", minute];
} else if ([[dateType objectAtIndex:x] isEqualToString:@"s"]) {
format = [format stringByAppendingFormat:@"%d", second];
}
} else {
if ([[dateType objectAtIndex:x] isEqualToString:@"Y"]) {
format = [format stringByAppendingFormat:@"%d|", year];
} else if ([[dateType objectAtIndex:x] isEqualToString:@"M"]) {
format = [format stringByAppendingFormat:@"%d|", month];
} else if ([[dateType objectAtIndex:x] isEqualToString:@"D"]) {
format = [format stringByAppendingFormat:@"%d|", day];
} else if ([[dateType objectAtIndex:x] isEqualToString:@"W"]) {
format = [format stringByAppendingFormat:@"%d|", weekday];
} else if ([[dateType objectAtIndex:x] isEqualToString:@"h"]) {
format = [format stringByAppendingFormat:@"%d|", hour];
} else if ([[dateType objectAtIndex:x] isEqualToString:@"m"]) {
format = [format stringByAppendingFormat:@"%d|", minute];
} else if ([[dateType objectAtIndex:x] isEqualToString:@"s"]) {
format = [format stringByAppendingFormat:@"%d|", second];
}
}
if ([[dateType objectAtIndex:x] isEqualToString:@"xx"]) {
format = [NSString stringWithFormat:@"Year: %d, Month: %d, Day: %d, Weekday: %d, Hour: %d, Minute: %d, Second: %d", year, month, day, weekday, hour, minute, second];
}
}
} else {
format = [format stringByAppendingFormat:@"%d|", year];
format = [format stringByAppendingFormat:@"%d|", month];
format = [format stringByAppendingFormat:@"%d|", day];
format = [format stringByAppendingFormat:@"%d|", weekday];
format = [format stringByAppendingFormat:@"%d|", hour];
format = [format stringByAppendingFormat:@"%d|", minute];
format = [format stringByAppendingFormat:@"%d|", second];
format = [NSString stringWithFormat:@"%d|%d|%d|%d|%d|%d|%d", year, month, day, weekday, hour, minute, second];
}
result = format;
return result;
}
示例强>
NSDate * date = [self getDateWithMonth:12 day:24 year:1994];
NSString * dateInString = [self getStringFromDate:date dateType:DATE_TYPE_MMDDYYYY];
int month = [[[dateInString componentsSeparatedByString:@"|"] objectAtIndex:0] intValue];
int day = [[[dateInString componentsSeparatedByString:@"|"] objectAtIndex:1] intValue];
int year = [[[dateInString componentsSeparatedByString:@"|"] objectAtIndex:2] intValue];
NSLog(@"String of Date: \"%@\"", dateInString);
NSLog(@"Month: %d", month);
NSLog(@"Day: %d", day);
NSLog(@"Year: %d", year);
方法[self getDateWithMonth:12 day:24 year:1994]
返回一个通常难以阅读的NSDate
对象,因此您可以使用[self getStringFromDate:date dateType:DATE_TYPE_MMDDYYYY]
来获取NSDate
对象的字符串。
使用定义(宏)指定您希望在字符串中获取的日期格式。
例如:
DATE_TYPE_hhmmss
会返回Hour|Minute|Second
,
DATE_TYPE_MMDDYYYY
会返回Month|Day|Year
,
DATE_TYPE_MMDDYYYYhhmmss
会返回Month|Day|Year|Hour|Minute|Second
,
DATE_TYPE_MMDDYYYYWWhhmmss
将返回Month|Day|Year|Weekday (#)|Hour|Minute|Second
依旧......
控制台日志
2012-04-29 13:42:15.791 Atomic Class[1373:f803] String of Date: "12|24|1994"
2012-04-29 13:42:15.793 Atomic Class[1373:f803] Month: 12
2012-04-29 13:42:15.794 Atomic Class[1373:f803] Day: 24
2012-04-29 13:42:15.794 Atomic Class[1373:f803] Year: 1994