通过组合两个 NSDate 来创建一个新的 NSDate

发布于 2024-10-22 05:45:14 字数 126 浏览 3 评论 0原文

我有 NSDate* day1NSDate* day2。如何通过仅复制第 1 天的年/月/日和第 2 天的小时/分钟/秒来制作 NSDate* day3

I have NSDate* day1 and NSDate* day2. How can I make a NSDate* day3 by copying only year/month/day from day1 and hour/minute/second from day2?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

下雨或天晴 2024-10-29 05:45:14
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSDateComponents *comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:day1];
NSDate *day3 = [[NSCalendar currentCalendar] dateFromComponents:comps];

unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:day2];
day3 = [[NSCalendar currentCalendar] dateByAddingComponents:comps toDate:day3 options:0];

来源:

  1. NSDateComponents 类参考< /a>
  2. NSCalendar 类参考
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSDateComponents *comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:day1];
NSDate *day3 = [[NSCalendar currentCalendar] dateFromComponents:comps];

unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:day2];
day3 = [[NSCalendar currentCalendar] dateByAddingComponents:comps toDate:day3 options:0];

Sources:

  1. NSDateComponents Class Reference
  2. NSCalendar Class Reference
浪推晚风 2024-10-29 05:45:14

它不像 darvids0n 那样紧凑,但它可以工作。

NSDate* day1 = [datePicker dateValue];
NSDate* day2 = [NSDate date];

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:day1;
NSInteger day = [components day];    
NSInteger month = [components month];
NSInteger year = [components year];

components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit fromDate:day2];
NSInteger hour = [components hour];    
NSInteger minute = [components minute];
NSInteger second = [components second];

NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
[comps setDay:day];
[comps setMonth:month];
[comps setYear:year];
[comps setHour:hour];
[comps setMinute:minute];
[comps setSecond:second];
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *day3 = [cal dateFromComponents:comps];

或者

NSDateFormatter *tempFormatter = [[[NSDateFormatter alloc]init]autorelease];
[tempFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString* message = [NSString stringWithFormat:@"%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second];
NSDate *day3 = [tempFormatter dateFromString:message];

It's not as compact as darvids0n's, but it works.

NSDate* day1 = [datePicker dateValue];
NSDate* day2 = [NSDate date];

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:day1;
NSInteger day = [components day];    
NSInteger month = [components month];
NSInteger year = [components year];

components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit fromDate:day2];
NSInteger hour = [components hour];    
NSInteger minute = [components minute];
NSInteger second = [components second];

NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
[comps setDay:day];
[comps setMonth:month];
[comps setYear:year];
[comps setHour:hour];
[comps setMinute:minute];
[comps setSecond:second];
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *day3 = [cal dateFromComponents:comps];

or

NSDateFormatter *tempFormatter = [[[NSDateFormatter alloc]init]autorelease];
[tempFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString* message = [NSString stringWithFormat:@"%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second];
NSDate *day3 = [tempFormatter dateFromString:message];
烈酒灼喉 2024-10-29 05:45:14

您需要创建一个 NSCalendar,然后从第一个和第二个日期获取日期组件,然后使用 NSCalendar 中的新日期组件获取日期。

使用 NSCalendar 方法

– components:fromDate:
– dateFromComponents:

和相应的单位标志

NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit; 

You need to create an NSCalendar and then get the date components from the first and second dates and then get the date using the new date components from the NSCalendar.

Use the NSCalendar methods

– components:fromDate:
– dateFromComponents:

and the corresponding unit flags

NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit; 
独闯女儿国 2024-10-29 05:45:14

作为记录,这是一种更通用的方法,

@implementation NSDate (Additions)

- (void)assignFlags:(NSUInteger)unitFlags to:(NSDateComponents *)components {
  NSDateComponents *myComponents = 
        [[NSCalendar currentCalendar] components: unitFlags
                                        fromDate: self];
  if (unitFlags & NSYearCalendarUnit)
    [components setYear: [myComponents year]];
  if (unitFlags & NSMonthCalendarUnit)
    [components setMonth: [myComponents month]];
  if (unitFlags & NSDayCalendarUnit)
    [components setDay: [myComponents day]];
  if (unitFlags & NSHourCalendarUnit)
    [components setHour: [myComponents hour]];
  if (unitFlags & NSMinuteCalendarUnit)
    [components setMinute: [myComponents minute]];
  if (unitFlags & NSSecondCalendarUnit)
    [components setSecond: [myComponents second]];
}

- (NSDate *)dateByTakingComponents:(NSUInteger)unitFlags fromDate:(NSDate *)date {

  NSUInteger allFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

  NSDateComponents *allComponents = [[NSDateComponents alloc] init];
  [date assignFlags: unitFlags to: allComponents];
  [self assignFlags: allFlags & ~unitFlags to: allComponents];

  NSDate *result = [[NSCalendar currentCalendar] dateFromComponents: allComponents];
  [allComponents release];

  return result;
}

@end

您可以这样称呼它

NSDate *day3 = 
    [day2 dateByTakingComponents: NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                        fromDate: day1];

For the records, here is a more general approach

@implementation NSDate (Additions)

- (void)assignFlags:(NSUInteger)unitFlags to:(NSDateComponents *)components {
  NSDateComponents *myComponents = 
        [[NSCalendar currentCalendar] components: unitFlags
                                        fromDate: self];
  if (unitFlags & NSYearCalendarUnit)
    [components setYear: [myComponents year]];
  if (unitFlags & NSMonthCalendarUnit)
    [components setMonth: [myComponents month]];
  if (unitFlags & NSDayCalendarUnit)
    [components setDay: [myComponents day]];
  if (unitFlags & NSHourCalendarUnit)
    [components setHour: [myComponents hour]];
  if (unitFlags & NSMinuteCalendarUnit)
    [components setMinute: [myComponents minute]];
  if (unitFlags & NSSecondCalendarUnit)
    [components setSecond: [myComponents second]];
}

- (NSDate *)dateByTakingComponents:(NSUInteger)unitFlags fromDate:(NSDate *)date {

  NSUInteger allFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

  NSDateComponents *allComponents = [[NSDateComponents alloc] init];
  [date assignFlags: unitFlags to: allComponents];
  [self assignFlags: allFlags & ~unitFlags to: allComponents];

  NSDate *result = [[NSCalendar currentCalendar] dateFromComponents: allComponents];
  [allComponents release];

  return result;
}

@end

You can call it like this

NSDate *day3 = 
    [day2 dateByTakingComponents: NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                        fromDate: day1];
蓝眼泪 2024-10-29 05:45:14

更新 Swift 4 和4.1

 func combineDateAndTimeToOneDateForAPI() {

    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)

}

Update Swift 4 & 4.1

 func combineDateAndTimeToOneDateForAPI() {

    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)

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文