看来非常多网上关于日期的处理,今天、昨天、前天、后天、一周等,满心欢喜的拿着去验证结果总是不尽如人意,看别别人的代码看的脑涨。自己就写一个简单的,自己来用,以后用其它的方法,我会在完好,再次记录。以方便以后查阅
--宋飞祥 2014.7.19
直接上代码,非常easy理解:
#import
@interface MyCutomNSDateDeal : NSObject
+(NSInteger)timeZoneOffset;
+(NSInteger)preDayOfTheDate_start:(NSDate *)date;
+(NSInteger)preDayOfTheDate_end:(NSDate *)date;
+(NSInteger)nextDayOfTheDate_start:(NSDate *)date;
+(NSInteger)nextDayOfTheDate_end:(NSDate *)date;
@end
#import "MyCutomNSDateDeal.h"
@implementation MyCutomNSDateDeal
+(NSInteger)timeZoneOffset
{
NSDate *date = [NSDate date];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger offset = [zone secondsFromGMTForDate:date];
return offset;
}
/**
* 依据给出的日期,返回该日期的前一天的起点(相对于日期起点。seconds的计数)
*
* @param date 日期
*
* @return 返回的是seconds计数,相对于日历系统起始至该日期前一天的起点(就是0点)
*/
+(NSInteger)preDayOfTheDate_start:(NSDate *)date
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:(NSHourCalendarUnit | NSMinuteCalendarUnit|NSSecondCalendarUnit) fromDate:date];
[components setHour:-[components hour]-24];
[components setMinute:-[components minute]];
[components setSecond:-[components second]];
NSDate *date1 = [cal dateByAddingComponents:components toDate:date options:0];
return [date1 timeIntervalSince1970]+[self timeZoneOffset];
}
/**
* 依据给出的日期。返回该日期的前一天的终点(也就是给出日期的起点)
*
* @param date 给定的日期
*
* @return 返回的是seconds计数
*/
+(NSInteger)preDayOfTheDate_end:(NSDate *)date
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:(NSHourCalendarUnit | NSMinuteCalendarUnit|NSSecondCalendarUnit) fromDate:date];
[components setHour:-[components hour]];
[components setMinute:-[components minute]];
[components setSecond:-[components second]];
NSDate *date1 = [cal dateByAddingComponents:components toDate:date options:0];
return [date1 timeIntervalSince1970]+[self timeZoneOffset];
}
+(NSInteger)nextDayOfTheDate_start:(NSDate *)date
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:(NSHourCalendarUnit | NSMinuteCalendarUnit|NSSecondCalendarUnit) fromDate:date];
[components setHour:-[components hour]+24];
[components setMinute:-[components minute]];
[components setSecond:-[components second]];
NSDate *date1 = [cal dateByAddingComponents:components toDate:date options:0];
return [date1 timeIntervalSince1970]+[self timeZoneOffset];
}
+(NSInteger)nextDayOfTheDate_end:(NSDate *)date
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:(NSHourCalendarUnit | NSMinuteCalendarUnit|NSSecondCalendarUnit) fromDate:date];
[components setHour:-[components hour]+ 2*24];
[components setMinute:-[components minute]];
[components setSecond:-[components second]];
NSDate *date1 = [cal dateByAddingComponents:components toDate:date options:0];
return [date1 timeIntervalSince1970]+[self timeZoneOffset];
}
//+(NSInteger)preWeekOfTheDate_
@end
使用例如以下:
这里我取得date 为当天的,用户能够换成自己想要的相对日期就可以!!
NSDate *today = [NSDate date];
NSLog(@"today ::::: %@",today);
NSInteger today_int = [MyCutomNSDateDeal preDayOfTheDate_start:today];
NSDate *per_start = [NSDate dateWithTimeIntervalSince1970:today_int];
NSLog(@"per_start _:_ %@",per_start);
NSDate *per_end = [NSDate dateWithTimeIntervalSince1970:[MyCutomNSDateDeal preDayOfTheDate_end:today]];
NSLog(@"per_end -:- %@",per_end);
NSDate *next_start = [NSDate dateWithTimeIntervalSince1970:[MyCutomNSDateDeal nextDayOfTheDate_start:today]];
NSDate *next_end = [NSDate dateWithTimeIntervalSince1970:[MyCutomNSDateDeal nextDayOfTheDate_end:today]];
NSLog(@"next_start === %@",next_start);
NSLog(@"next_end === %@",next_end);
打印结果例如以下:
2014-07-19 15:26:30.565 FMDBSortByDateDemo[4423:60b] today ::::: 2014-07-19 07:26:30 +0000
2014-07-19 15:26:30.566 FMDBSortByDateDemo[4423:60b] per_start _:_ 2014-07-18 00:00:00 +0000
2014-07-19 15:26:30.566 FMDBSortByDateDemo[4423:60b] per_end -:- 2014-07-19 00:00:00 +0000
2014-07-19 15:26:30.566 FMDBSortByDateDemo[4423:60b] next_start === 2014-07-20 00:00:00 +0000
2014-07-19 15:26:30.567 FMDBSortByDateDemo[4423:60b] next_end === 2014-07-21 00:00:00 +0000
第一行打印能够看出,我们打印的时间理我如今真正须要的时间差8个小时。这就是时区的偏移,加上8个小时就对了,以下的打印在我定义的头文件里,已经把相对偏加上了!
这里我想说的是:我项目中用到要存数据和date,而数据库(FMDB)是不支持date格式的。我採取的是把相对日历起点的seconds(秒)存起来。通过比对秒数来取想要的数据。
假设大家有好的方法,能够交流!!
以下是我的数据库取值过程:
NSInteger inter = [MyCutomNSDateDeal preDayOfTheDate_end:today];
NSString *sql = [NSString stringWithFormat:@"SELECT * FROM Temperature WHERE Date > %i ",inter];
// NSString *sql = [NSString stringWithFormat:@"SELECT * FROM Temperature WHERE Date between %li and %li",from,to];
if ([db open]) {
FMResultSet *rs = [db executeQuery:sql];
while ([rs next]) {
NSString *name = [rs stringForColumn:@"Name"];
NSInteger time = [rs intForColumn:@"Date"];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
float temp = [rs doubleForColumn:@"Temperature"];
NSString *event = [rs stringForColumn:@"Event"];
NSLog(@"Name:%@,Date:%@,Temperature:%f,Event:%@",name,date,temp,event);
}
}
两个sql语句都验证了。是正确的,有一点我要说的是,我们在处理取出的数据时,加上了偏移量。所以我们存取数据时。也要加上偏移量。应该例如以下:
-(void)temperatureAndDateUpdate:(float)value
{
NSDate *date = [NSDate date];
NSInteger time = [date timeIntervalSince1970];
time = time + [MyCutomNSDateDeal timeZoneOffset]; //加上时区偏移量
NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:time];
NSLog(@"temperature update date1 === %@",date1);
if ([db open]) {
[db executeUpdate:@"INSERT INTO Temperature(Name,Date,Temperature,Event) VALUES (?,?
,?,?)",@"SFX",[NSNumber numberWithInteger:time],[NSNumber numberWithFloat:value],@"吃药"]; NSLog(@"success!!"); }else { NSLog(@"error: %@ %@",[db lastError],[db lastErrorMessage]); } [db close]; }