如何将第一个 NSDate 的日期和第二个 NSDate 的时间组合成一个 NSDate?
How to compose date from first NSDate and time from second NSDate into one NSDate?
我需要一件事:
- 创建 一个 输出
NSDate
包括它们的格式:"yyyy-MM-dd HH:mm:00"
,其中 yyyy-MM-dd
来自第一个 NSDate
和 HH:mm:00
来自第二个 NSDate
使用了这个代码
+ (NSDate *)combineDate:(NSDate *)date withTime:(NSDate *)time
{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned unitFlagsDate = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *dateComponents = [gregorian components:unitFlagsDate fromDate:date];
unsigned unitFlagsTime = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *timeComponents = [gregorian components:unitFlagsTime fromDate:time];
[dateComponents setSecond:[timeComponents second]];
[dateComponents setHour:[timeComponents hour]];
[dateComponents setMinute:[timeComponents minute]];
NSDate *combDate = [gregorian dateFromComponents:dateComponents];
return combDate;
}
可能对你有帮助
假设你有两个字符串:
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
[dateformate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *outputDate = [dateformate dateFromString:[@"2015-09-01" stringByAppendingString:@"08:33:00"]];
您可以通过创建便利初始化程序轻松实现此目的,如下所示:
convenience init(date: NSDate?, time: NSDate?) {
var combinedDate = NSDate()
if let date = date {
if let time = time {
let calendar = NSCalendar.currentCalendar()
let timeComponents = calendar.components(.HourCalendarUnit | .MinuteCalendarUnit | .TimeZoneCalendarUnit, fromDate: time)
var dateComponents = calendar.components(.YearCalendarUnit | .MonthCalendarUnit | .DayCalendarUnit, fromDate: date)
dateComponents.hour = timeComponents.hour
dateComponents.minute = timeComponents.minute
dateComponents.second = 0
dateComponents.timeZone = timeComponents.timeZone
if let dateFromComponents = calendar.dateFromComponents(dateComponents) {
combinedDate = dateFromComponents
}
}
}
self.init(timeInterval:0, sinceDate: combinedDate)
}
基本上它由两个日期的组件组成一个日期。例如,从第二个 NSDate
调用 time 开始计算小时、分钟和秒,从第一个 NSDate
调用 [=19] 计算年、月和日=]日期.
您可以修改组件以提高或降低准确度。您甚至可以扩展初始化程序以包含日历的自定义单位。
Swift 5 版本:
init?(date: Date, time: Date) {
let calendar = Calendar.current
let timeComponents = calendar.dateComponents([.hour, .minute, .timeZone], from: time)
var dateComponents = calendar.dateComponents([.year, .month, .day], from: date)
dateComponents.hour = timeComponents.hour
dateComponents.minute = timeComponents.minute
dateComponents.second = 0
dateComponents.timeZone = timeComponents.timeZone
guard let combined = calendar.date(from: dateComponents) else { return nil }
self = combined
}
我需要一件事:
- 创建 一个 输出
NSDate
包括它们的格式:"yyyy-MM-dd HH:mm:00"
,其中yyyy-MM-dd
来自第一个NSDate
和HH:mm:00
来自第二个NSDate
使用了这个代码
+ (NSDate *)combineDate:(NSDate *)date withTime:(NSDate *)time
{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned unitFlagsDate = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *dateComponents = [gregorian components:unitFlagsDate fromDate:date];
unsigned unitFlagsTime = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *timeComponents = [gregorian components:unitFlagsTime fromDate:time];
[dateComponents setSecond:[timeComponents second]];
[dateComponents setHour:[timeComponents hour]];
[dateComponents setMinute:[timeComponents minute]];
NSDate *combDate = [gregorian dateFromComponents:dateComponents];
return combDate;
}
可能对你有帮助
假设你有两个字符串:
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
[dateformate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *outputDate = [dateformate dateFromString:[@"2015-09-01" stringByAppendingString:@"08:33:00"]];
您可以通过创建便利初始化程序轻松实现此目的,如下所示:
convenience init(date: NSDate?, time: NSDate?) {
var combinedDate = NSDate()
if let date = date {
if let time = time {
let calendar = NSCalendar.currentCalendar()
let timeComponents = calendar.components(.HourCalendarUnit | .MinuteCalendarUnit | .TimeZoneCalendarUnit, fromDate: time)
var dateComponents = calendar.components(.YearCalendarUnit | .MonthCalendarUnit | .DayCalendarUnit, fromDate: date)
dateComponents.hour = timeComponents.hour
dateComponents.minute = timeComponents.minute
dateComponents.second = 0
dateComponents.timeZone = timeComponents.timeZone
if let dateFromComponents = calendar.dateFromComponents(dateComponents) {
combinedDate = dateFromComponents
}
}
}
self.init(timeInterval:0, sinceDate: combinedDate)
}
基本上它由两个日期的组件组成一个日期。例如,从第二个 NSDate
调用 time 开始计算小时、分钟和秒,从第一个 NSDate
调用 [=19] 计算年、月和日=]日期.
您可以修改组件以提高或降低准确度。您甚至可以扩展初始化程序以包含日历的自定义单位。
Swift 5 版本:
init?(date: Date, time: Date) {
let calendar = Calendar.current
let timeComponents = calendar.dateComponents([.hour, .minute, .timeZone], from: time)
var dateComponents = calendar.dateComponents([.year, .month, .day], from: date)
dateComponents.hour = timeComponents.hour
dateComponents.minute = timeComponents.minute
dateComponents.second = 0
dateComponents.timeZone = timeComponents.timeZone
guard let combined = calendar.date(from: dateComponents) else { return nil }
self = combined
}