NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];
NSString *theDate = [dateFormat stringFromDate:date];
time_t unixTime = (time_t) [date timeIntervalSince1970];
我需要没有小时,分钟和秒的unixTime
答案 0 :(得分:0)
您可能希望从原始日期中提取重要日期组件
NSDate *date = [NSDate date];
NSCalendar *gregorianCalendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *componentsDate = [gregorianCalendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date];
然后,与他们建立一个新约会
NSDateComponents *toDateComponents = [NSDateComponents new];
[toDateComponents setYear:[componentsDate year]];
[toDateComponents setMonth:[componentsDate month]];
[toDateComponents setDay:[componentsDate day]];
[toDateComponents setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
NSDate *toDate = [gregorianCalendar dateFromComponents:toDateComponents];
最后,获取时间间隔
NSTimeInterval toDateTimeInterval = [toDate timeIntervalSince1970];
答案 1 :(得分:0)
或者从另一个方向来到它,并在Swift:
// Get the current date and time
let theDate = NSDate()
// Get the current hours, minutes and seconds from "now"
let theCalendar = NSCalendar.currentCalendar()
let theComponents = theCalendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond, fromDate: theDate)
// Construct a new time, the equivalent number of seconds earlier than "now". This is effectively midnight that passed
let theCorrectedDate = NSDate( timeInterval: -(NSTimeInterval)((theComponents.hour * 3600) + (theComponents.minute * 60) + theComponents.second), sinceDate: theDate )
// And that gives you the unix time you want
let theUnixTime = theCorrectedDate.timeIntervalSince1970