开发者

Removing seconds from UnixTime Value

开发者 https://www.devze.com 2023-02-12 05:46 出处:网络
So I have a unixtime datetime stamp. For arguments sake, lets say it\'s now at 23:49:24 on 21/02/2011 (GMT). This would be:

So I have a unixtime datetime stamp. For arguments sake, lets say it's now at 23:49:24 on 21/02/2011 (GMT). This would be:

1298332164

Now, is there anyway to remove the seconds component from this?开发者_StackOverflow中文版 I'm writing this in Obj-C so currently I have:

NSDate *todayNow = [NSDate date];
int unixtimeNow = [todayNow timeIntervalSince1970];

And would end up with:

1298332140

Thanks


In Cocoa, you can do it like this:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger calendarUnits = NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
NSDateComponents *dateComps = [calendar components:calendarUnits fromDate:todayNow];
[dateComps setSecond:0];
NSDate *todayNowNoSeconds = [calendar dateFromComponents:dateComps];


What about 1298332164 / 60 * 60 ?


NSTimeInterval seconds =  fmod([self timeIntervalSince1970], 60);    
NSDate *secondsTrimmedDtae =  [self dateByAddingTimeInterval:-seconds];

Note self refers to NSDate instance you have to replace self with your date obj.

0

精彩评论

暂无评论...
验证码 换一张
取 消