iphone NSDate - 获取今天的日期并强制时间
希望对某人来说是一个快速的!我很难理解为什么下面的代码不起作用。我试图获取今天的日期(例如:2010-09-10)并手动添加时间。不过,它似乎总是返回 00:00:00。有什么想法我哪里出错了吗?
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = @"yyyy-MM-dd";
NSString *actualDate = [dateFormatter stringFromDate:[NSDate date]];
NSDate *sDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@ %@", actualDate, @"01:00:00"]];
NSDate *eDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@ %@", actualDate, @"23:59:59"]];
NSDate 对象始终返回格式 2010-09-10 00:00:00 +01:00。有些地方只是没有时间......有什么想法吗?非常感谢。
* 更新 *
下面的代码有效。我按照建议进行操作并访问了这些元素并更新了它们。
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [calendar components:unitFlags fromDate:date];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = @"yyyy-MM-dd hh:mm:ss";
//update for the start date
[comps setHour:0];
[comps setMinute:0];
[comps setSecond:0];
NSDate *sDate = [calendar dateFromComponents:comps];
//update for the end date
[comps setHour:23];
[comps setMinute:59];
[comps setSecond:59];
NSDate *eDate = [calendar dateFromComponents:comps];
hopefully a quick one for someone! I am struggling to see why the code below isn't working. I am trying to get today's date (eg: 2010-09-10) and manually adding the time. It always seems to return 00:00:00 for the time though. any ideas where I'm going wrong?
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = @"yyyy-MM-dd";
NSString *actualDate = [dateFormatter stringFromDate:[NSDate date]];
NSDate *sDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@ %@", actualDate, @"01:00:00"]];
NSDate *eDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@ %@", actualDate, @"23:59:59"]];
The NSDate objects are returning the format 2010-09-10 00:00:00 +01:00 all the time. Somewhere it's just not picking up the time.... any ideas? many thanks.
* UPDATE *
The code below works. I did as suggested and accessed the elements and updated them.
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [calendar components:unitFlags fromDate:date];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = @"yyyy-MM-dd hh:mm:ss";
//update for the start date
[comps setHour:0];
[comps setMinute:0];
[comps setSecond:0];
NSDate *sDate = [calendar dateFromComponents:comps];
//update for the end date
[comps setHour:23];
[comps setMinute:59];
[comps setSecond:59];
NSDate *eDate = [calendar dateFromComponents:comps];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用
NSCalendar
来代替。它允许您解析当前日期并修改其组件。就像您可以使用今天的日期初始化它,然后设置时间,然后从中获取NSDate
。(旧答案:因为你的格式化程序被配置为仅识别日期部分。将时间部分添加到其中(即使它的名称是 NSDateFormatter,它会很乐意解析日期和时间),你也会看到时间。)
You should use
NSCalendar
instead then. It allows you to parse the current date and modify its components. Like you can initialize it with today's date and then set the time and then get anNSDate
from it.(Old answer: Because your formatter is configured to only recognize the date part. Add the time part to it (even though its name is NSDateFormatter, it will happily parse dates and times) and you will see the time too.)
我今天在想做同样的事情时发现了这个问题,创建一个具有今天特定时间的 NSDate 。提供的解决方案并不完全是我所需要的,所以我想出了这个:
I found this question today while looking to do the same thing, create an NSDate with a specific time today. The solution presented wasn't quite what I needed so I came up with this: