Iphone 如何从约会中减去30分钟
我正在获取日期值日期:2013-06-07 18:30:00+0000。我正试图从中减去30分钟,但它没有给出适当的时间 我试过这个Iphone 如何从约会中减去30分钟,iphone,ios,nsdate,Iphone,Ios,Nsdate,我正在获取日期值日期:2013-06-07 18:30:00+0000。我正试图从中减去30分钟,但它没有给出适当的时间 我试过这个 NSTimeInterval secondsPerHour = 60*30; NSDate *newDate = [date dateByAddingTimeInterval:-secondsPerHour]; 你可以试试,像这样的: NSDate* yourDate = [NSDate dateWithTimeIntervalSinceNow:0]; //
NSTimeInterval secondsPerHour = 60*30;
NSDate *newDate = [date dateByAddingTimeInterval:-secondsPerHour];
你可以试试,像这样的:
NSDate* yourDate = [NSDate dateWithTimeIntervalSinceNow:0]; // your date, to substract 30 mins
NSDateComponents *dc = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:yourDate];
[dc setMinute:[dc minute] - 30];
NSDate* date = [[NSCalendar currentCalendar] dateFromComponents:dc]; // 30 mins before now
或者:
NSDate* yourDate = [NSDate dateWithTimeIntervalSinceNow:0]; // your date, to substract 30 mins
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setMinute:-30];
NSDate* date = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:yourDate options:0]; // 30 mins before now
您可以使用:
NSDate *minusOneHr = [[NSDate date] dateByAddingTimeInterval:-30*60];
原因:无符号整数的
负数
是一个正数
。这应该起作用:
NSDate *newDate = [date dateByAddingTimeInterval:-(NSTimeInterval)((u_int32_t)30*60)];
编辑:可选使用NSDateComponents
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMinute:-30];
NSDate *halfHourAgo = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:yourDateHere options:0];
NSLog(@"\ncurrentDate: %@ \n halfHourAgo: %@", yourDateHere, halfHourAgo);
在viewDidLoad中复制粘贴此代码并首先检查结果
NSDate *today = [[NSDate alloc] init];
NSLog(@"Today %@", today);
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setMinute:-30]; // If you want to change Month,Year,Day,Hour etc. Just change the "setMinute" to setMonth,setYear,setDay,setHour
NSDate *dateComp = [gregorian dateByAddingComponents:offsetComponents toDate:today options:0];
NSLog(@"Desired Day %@", dateComp);
斯威夫特5
let minus30Min = Date().addingTimeInterval(-30 * 60)
我认为这与问题中给出的代码相同。只需切换数值的位置仔细阅读问题,然后发表评论,用户希望减去分钟数,因此它不取决于月内有多少天低于tofa?????
let minus30Min = Date().addingTimeInterval(-30 * 60)