NSDate 带有来自一个 NSDate 的日期组件和来自另一个 NSDate 的时间组件
Posted
技术标签:
【中文标题】NSDate 带有来自一个 NSDate 的日期组件和来自另一个 NSDate 的时间组件【英文标题】:NSDate with date component from one and time component from another NSDate 【发布时间】:2012-06-26 05:40:01 【问题描述】:我的应用中有两个 UIDatePicker 控件,一个配置了 UIDatePickerModeDate,一个配置了 UIDatePickerModeTime。
我想用 UIDatePicker 第一个的日期和第二个的时间配置一个 NSDate 对象......如何?
谢谢, 标记
【问题讨论】:
【参考方案1】:假设 date1 和 date2 是您的两个 NSDate 值。 date1 包含您的日期组件,而 date2 包含您的时间组件。以下是您将它们组合在一起的方法:
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// Extract date components into components1
NSDateComponents *components1 = [gregorianCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
fromDate:date1];
// Extract time components into components2
NSDateComponents *components2 = [gregorianCalendar components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit
fromDate:date2];
// Combine date and time into components3
NSDateComponents *components3 = [[NSDateComponents alloc] init];
[components3 setYear:components1.year];
[components3 setMonth:components1.month];
[components3 setDay:components1.day];
[components3 setHour:components2.hour];
[components3 setMinute:components2.minute];
[components3 setSecond:components2.second];
// Generate a new NSDate from components3.
NSDate *combinedDate = [gregorianCalendar dateFromComponents:components3];
// combinedDate contains both your date and time!
适用于 iOS 8.0 +
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
// Extract date components into components1
NSDateComponents *components1 = [gregorianCalendar components:(NSCalendarUnit)(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
fromDate:date1];
// Extract time components into components2
NSDateComponents *components2 = [gregorianCalendar components:(NSCalendarUnit)(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond)
fromDate:date2];
// Combine date and time into components3
NSDateComponents *components3 = [[NSDateComponents alloc] init];
[components3 setYear:components1.year];
[components3 setMonth:components1.month];
[components3 setDay:components1.day];
[components3 setHour:components2.hour];
[components3 setMinute:components2.minute];
[components3 setSecond:components2.second];
// Generate a new NSDate from components3.
NSDate * combinedDate = [gregorianCalendar dateFromComponents:components3];
// combinedDate contains both your date and time!
【讨论】:
【参考方案2】:在斯威夫特中:
let calendar = NSCalendar.currentCalendar()
let date1 = NSDate() // PickerDateMode
let date2 = NSDate() // PickerHourMode
let componentsOne = calendar.components([.Year, .Month, .Day], fromDate: date1)
let componentsTwo = calendar.components([.Hour, .Minute], fromDate: date2)
let totalComponents = NSDateComponents()
totalComponents.year = componentsOne.year
totalComponents.month = componentsOne.month
totalComponents.day = componentsOne.day
totalComponents.hour = componentsTwo.hour
totalComponents.minute = componentsTwo.minute
let finalDate = calendar.dateFromComponents(totalComponents)
【讨论】:
【参考方案3】:NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDate *date = [NSDate date];//datapicker date
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];
NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
NSInteger day = [dateComponents day];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];
【讨论】:
【参考方案4】:更新到 Swift 4 和 4.1
func combineDateAndTimeToOneDate()
let datefrmter = DateFormatter()
datefrmter.dateFormat = "dd/MM/yyyy"
let dateFromString = datefrmter.date(from: lblDate.text!)
datefrmter.dateFormat = "h:mm a"
let timeFromString = datefrmter.date(from: lblTime.text!)
let calendar = NSCalendar.current
let componentsOne = calendar.dateComponents([.year,.month,.day], from: dateFromString!)
let componentsTwo = calendar.dateComponents([.hour,.minute,.second,.nanosecond], from: timeFromString!)
let totalComponents = NSDateComponents()
totalComponents.year = componentsOne.year!
totalComponents.month = componentsOne.month!
totalComponents.day = componentsOne.day!
totalComponents.hour = componentsTwo.hour!
totalComponents.minute = componentsTwo.minute!
totalComponents.second = componentsTwo.second!
totalComponents.nanosecond = componentsTwo.nanosecond!
let finalDate = calendar.date(from: totalComponents as DateComponents)
print(finalDate)
希望对你有帮助!
【讨论】:
你不需要使用NSDateComponents
最后转换为DateComponents
,可以使用DateComponents
,只需将totalComponents改为var而不是let以上是关于NSDate 带有来自一个 NSDate 的日期组件和来自另一个 NSDate 的时间组件的主要内容,如果未能解决你的问题,请参考以下文章
NSDateComponents - 结合日期和时间会损失 1 小时?
如何在 [NSDate 日期] 当天 00:00 获取 NSDate?