构造一个NSDate从今天的日期和一个字符串与时间 [英] Constructing an NSDate from today's date and a string with the time

查看:125
本文介绍了构造一个NSDate从今天的日期和一个字符串与时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个表示时间的字符串,说10:45 am,并执行以下操作来解析字符串:

If I have a string representing a time, say "10:45 am", and do the following to parse the string:

NSDateFormatter *dateFormat;
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"h:mm a"];
NSLog(@"%@", [dateFormat dateFromString:@"10:45 AM"]);

我会得到这个记录:


2013-09-09 17:52:30.416 TimeTest [49491:a0b] 2000-01-01 15:45:00 +0000

2013-09-09 17:52:30.416 TimeTest[49491:a0b] 2000-01-01 15:45:00 +0000

如何在给定时间为当天创建 NSDate ?我试过这个

How can I create an NSDate for the current day at the given time? I tried this

NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"h:mm a"];
NSDate *time = [formatter dateFromString:@"10:45 AM"];
NSDateComponents *timeComponents = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit ) fromDate:time];
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date];
NSDateComponents *newComponents = [[NSDateComponents alloc]init];
newComponents.timeZone = [NSTimeZone systemTimeZone];
[newComponents setDay:[dateComponents year]];
[newComponents setMonth:[dateComponents month]];
[newComponents setYear:[dateComponents year]];
[newComponents setHour:[timeComponents hour]];
[newComponents setMinute:[timeComponents minute]];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *combinedDate = [gregorianCalendar dateFromComponents:newComponents];
NSLog(@"%@", combinedDate);

结果


2013-09-09 19:57:14.506 TimeTest [49712:a0b] 2019-03-06 15:45:00 +0000

2013-09-09 19:57:14.506 TimeTest[49712:a0b] 2019-03-06 15:45:00 +0000

我应该怎么做呢?

推荐答案

我不知道你在找什么。对于我的理解,你想要建立一个与当前年,月和日的日期,但通过从字符串解析你所提供的时间。
如果是这样,正如其他人所指出的,你需要使用NSDateComponents。

I'm not exactly sure what you are looking for. For what I understand, you want to build a date with the current year, month and day, but with your supplied time by parsing it from a string. If that is the case, as others have pointed out, you need to play with NSDateComponents.

根据你的代码,我写了这些行。他们应该通过合并两个日期来构建日期。

Based on your code I wrote these lines. They should build a date by merging two dates. The current one and the one you parsed.

// Get the full current date
NSDate *date = [NSDate date];

// Get the current calendar
NSCalendar *calendar = [NSCalendar currentCalendar];

// Split the date into components but only take the year, month and day and leave the rest behind
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date];

// Build the date formatter
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"h:mm a"];

// Convert the string time into an NSDate
NSDate *time = [formatter dateFromString:@"10:45 AM"];

// Split this one in components as well but take the time part this time
NSDateComponents *timeComponents = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit ) fromDate:time];

// Do some merging between the two date components
dateComponents.hour = timeComponents.hour;
dateComponents.minute = timeComponents.minute;

// Extract the NSDate object again
NSDate *result = [calendar dateFromComponents:dateComponents];

// Check if this was what you where looking for
NSLog(@"%@",result);

请注意,此示例代码是远未优化的。有更多的方法来获得你正在寻找的是使用时间间隔,但我觉得你想要一个肮脏的简单例子如何做组件复制和粘贴,然后提取日期。

Please be aware that this sample code is by far non-optimized. There are more crisp ways to obtain what you are looking for by using time intervals, but I felt like you wanted a dirty simple example on how to do components copy and paste and then extracting dates.

这篇关于构造一个NSDate从今天的日期和一个字符串与时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆