iphone NSDate - 获取今天的日期并强制时间

发布于 2024-09-18 20:14:54 字数 1460 浏览 3 评论 0原文

希望对某人来说是一个快速的!我很难理解为什么下面的代码不起作用。我试图获取今天的日期(例如: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

握住你手 2024-09-25 20:14:54

您应该使用 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 an NSDate 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.)

知足的幸福 2024-09-25 20:14:54

我今天在想做同样的事情时发现了这个问题,创建一个具有今天特定时间的 NSDate 。提供的解决方案并不完全是我所需要的,所以我想出了这个:

NSDate *                date;
NSString *              string, *timestamp;
NSDateFormatter *       formatter;

timestamp = @"17:30";

formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat: @"yyyy-MM-dd "];
[formatter setTimeZone: [NSTimeZone localTimeZone]];

string = [formatter stringFromDate: [NSDate date]];
string = [string stringByAppendingString: timestamp];

[formatter setDateFormat: @"yyyy-MM-dd HH:mm"];
date = [formatter dateFromString: string];

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:

NSDate *                date;
NSString *              string, *timestamp;
NSDateFormatter *       formatter;

timestamp = @"17:30";

formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat: @"yyyy-MM-dd "];
[formatter setTimeZone: [NSTimeZone localTimeZone]];

string = [formatter stringFromDate: [NSDate date]];
string = [string stringByAppendingString: timestamp];

[formatter setDateFormat: @"yyyy-MM-dd HH:mm"];
date = [formatter dateFromString: string];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文