【问题标题】:Create NSDate with a specific time创建具有特定时间的 NSDate
【发布时间】:2014-02-20 03:26:41
【问题描述】:

我有以下问题。我需要从特定时间创建一个 NSDate 对象。在给定的时区(有时是 CST、EST 等),时间是今天下午 6 点。不是每天下午 6 点,而是该特定时区的今天下午 6 点。

我正在考虑使用 NSCalendar 对象来创建特定的日期/时间,但问题是它需要一天、一个月和一年。那么当我没有正确的日期时如何设置这些值。示例:

今天是 2014 年 1 月 28 日,美国中部时间 11:09。现在,如果我需要创建一个 NSCalendar 对象,我需要日、月和年。我不能使用 NSDate 对象的日、月和年计算,因为它会导致边缘情况出现问题。

谁能指出我正确的方向?

【问题讨论】:

  • NSDateFormatter 将 NSString 转换为 NSDate... NSDateComponents 可能是另一种解决方案。
  • 如果您想要“今天”下午 6 点,请使用 NSDateFormatter 仅在本地时区格式化日期,附加“18:00:00”,然后使用另一个 NSDateFormatter 读取日期。

标签: ios objective-c nsdate nsdateformatter


【解决方案1】:

试试 NSDateComponents。

    // Initialize date components with date values
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    [dateComponents setYear:2014];
    [dateComponents setMonth:1];
    [dateComponents setDay:28];
    [dateComponents setHour:11];
    [dateComponents setMinute:9];

    NSCalendar *calendar = [[NSCalendar alloc]  initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate *configuredDate = [calendar dateFromComponents:dateComponents];

我不确定 initWithCalendarIndetifier 将采用哪些各种 NSCalendar 标识符配置选项,但如果这个答案有帮助,也许有人可以添加一些附加信息的编辑。

【讨论】:

    【解决方案2】:

    您的担忧似乎与边缘情况有关,这向我表明,您实际上不是在寻找“今天下午 6 点”,而是在寻找“下一个下午 6 点”,即如果时间已经过了下午 6 点,您希望明天下午 6 点?

    无论我的猜测是否正确,也许以下内容会有所帮助:

    1. 获取当前日期和时间 - NSDate
    2. 获取当前日历 - NSCalendar
    3. 将日历的时区设置为您想要的时区
    4. 使用日历将日期转换为组件 - NSDateComponents - 仅提取年、月、日、小时和时区的组件。您现在可以将目标时区中的当前时间分量向下舍入到最接近的小时数。
    5. 使用日历从提取的时间组件创建NSDate。现在,您的四舍五入时间为NSDate
    6. 使用您的组件计算要添加的小时数,以将时间提前到下一个下午 6 点。
    7. 将小时数转换为秒以生成NSTimeInterval 值,将该时间间隔添加到向下舍入的日期。 NSDate 将根据需要处理提前日期、更改月份等边缘情况。

    HTH

    【讨论】:

    • 很抱歉,由于声誉不足,我无法投票。谢谢,这正是我想要的。
    【解决方案3】:

    斯威夫特

    let calendar = Calendar(identifier: .gregorian)
    let units: Set<Calendar.Component> = [.year, .month, .day, .hour]
    var components = calendar.dateComponents(units, from: Date())
    components.hour = 18
    let todayAt6PM = calendar.date(from: components)
    

    目标 C

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSCalendarUnit calendarUnits = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour;
    NSDateComponents *components = [calendar components:calendarUnits fromDate:[NSDate date]];
    [components setHour:18];
    NSDate *todayAt6PM = [calendar dateFromComponents:components];
    

    【讨论】:

    • 问题是你如何初始化“现在”。如果我现在理解正确 = [NSDate date],对吗?但那是在UTC时间。我希望根据时区中的“现在”评估这些天数、小时数等
    【解决方案4】:

    您可以像这样从时间创建日期:

    NSDate *today = [NSDate date];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *weekdayComponents = [gregorian components:NSCalendarUnitDay fromDate:today];
    [weekdayComponents setHour:[timeOfDay intValue]];
    [weekdayComponents setMinute:00];
    

    Initialize a NSDate object with a specific time

    【讨论】:

    • 再次与上述相同的评论。今天是 [NSDate 日期],对吗?但这将是UTC时间。这些值会在大量边缘情况下动摇。
    【解决方案5】:

    或者,

    NSLocale *const here = [NSLocale currentLocale];
    NSDate *const now = [[NSDate alloc] init];
    NSCalendar *const calendar = [NSCalendar currentCalendar];
    [calendar setLocale: here];
    NSTimeZone *const timeZone = [NSTimeZone localTimeZone];
    [calendar setTimeZone: timeZone];
    NSDate *const midnight = [calendar startOfDayForDate: now];
    NSDate *sixOClock = [[NSDate date] initWithTimeInterval: 60 * 60 * (12 + 6) sinceDate: midnight];
    if ([now isGreaterThan: sixOClock]) {
        sixOClock = [[NSDate date] initWithTimeInterval: 60 * 60 * (12 + 6) + (24 * 60 * 60) sinceDate: midnight];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-06
      • 2018-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      • 2022-01-23
      • 1970-01-01
      相关资源
      最近更新 更多