gpt4 book ai didi

ios - 比较uipickerdate和当前日期

转载 作者:行者123 更新时间:2023-12-01 17:26:27 25 4
gpt4 key购买 nike

这是我为两个日期选择器提供的代码,需要使用if条件或开关将它们与当前日期进行比较。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.timeZone=[NSTimeZone defaultTimeZone];
dateFormatter.timeStyle=NSDateFormatterShortStyle;
dateFormatter.dateStyle=NSDateFormatterShortStyle;
NSString *dateTimeString=[dateFormatter stringFromDate:startTime.date];
NSLog(@"Start time is %@",dateTimeString);


NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init];
dateFormatter2.timeZone=[NSTimeZone defaultTimeZone];
dateFormatter2.timeStyle=NSDateFormatterShortStyle;
dateFormatter2.dateStyle=NSDateFormatterShortStyle;
NSString *dateTimeString2=[dateFormatter2 stringFromDate:endTime.date];
NSLog(@"End time is %@",dateTimeString2);

我的.h 中的
@property (weak, nonatomic) IBOutlet UIDatePicker *startTime;
@property (weak, nonatomic) IBOutlet UIDatePicker *endTime;

我怎么做?因为我只需要时间,我是否需要将它们存储到NSDate或NSTime?

另外, objective-c 是否存在If范围?

谢谢,

最佳答案

只需添加:

if ([[NSDate date] isEqualToDate:startTime.date]) {
NSLog(@"currentDate is equal to startTime");
}

if ([[NSDate date] isEqualToDate:endTime.date]) {
NSLog(@"currentDate is equal to endTime");
}

如果要计算两个日期之间的间隔,请使用此方法
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate


if([startTime.date timeIntervalSinceDate:[NSDate date]] > 0)
{
//start time greater than today
}

else if([startTime.date timeIntervalSinceDate:[NSDate date]] < 0)
{
//start time less than today
}

else
{
//both dates are equal
}

为了知道应用程序何时进入后台使用通知,请在您的viewDidLoad中添加此语句
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationEnteredBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];

添加功能以在通知发布时满足通知
-(void)applicationEnteredBackground
{
//do all the above steps here when you want.
}

并在该类的dealloc函数中删除观察者以进行通知
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

关于ios - 比较uipickerdate和当前日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15846329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com