标签: nsdatecomponents

NSDateComponents 将 UTC 日期转换为本地时区,搞乱了“今天”nsdate 比较

我正在尝试使用 NSDateComponents 来测试动态日期是否发生在“今天”。我用它来提取年/月/日,然后比较两个 NSDate 的这些值。理论上这应该有效。我在东海岸,所以东部时间,与 UTC 的距离为 -5。

当前实际时间:12:40:53,美国东部时间

以下是正确的 NSDate(UTC 时间)。时间是正确的 UTC:

  • 当前 [NSDate 日期]: 2016-01-19 17:40:53 +0000
  • 活动NSD日期:2016-01-19 00:00:00 +0000

这是我的日志,显示了每个日期。它将日期转换为本地时区 2016-01-19 07:00:00。由于当前日期位于一天中,因此 -5 小时偏移量不会导致日期发生变化。然而,午夜的时间却回到了一天前。我怎样才能让它尊重UTC?

NS日志:

2016-1-19 12:40:53 (America/New_York (EST) offset -18000), 
2016-1-18 19:0:0 (America/New_York (EST) offset -18000)
Run Code Online (Sandbox Code Playgroud)

代码:

NSDateComponents *current = [[NSCalendar currentCalendar] components:NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond |NSCalendarUnitTimeZone fromDate:_date];
NSDateComponents *activity = [[NSCalendar currentCalendar] components:NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond |NSCalendarUnitTimeZone fromDate:activityModel.date];

// …
Run Code Online (Sandbox Code Playgroud)

timezone objective-c nsdate nsdatecomponents ios

3
推荐指数
1
解决办法
1669
查看次数

获取单个NSDateComponent的2个日期之间的确切差异

如何获得2个值之间的精确差异(十进制)NSDate.

例如.2016年1月15日2017 7月15日 = 1.5年.

我可以使用类似的东西: NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitYear, fromDate: date1, toDate: date1, options: nil).year
但这给了我绝对的价值.即,对于上面的例子,它会给我1年.是否有可能将精确值正确至少几位小数?

nsdate nsdatecomponents ios swift

3
推荐指数
1
解决办法
752
查看次数

swift 3 获取最近的小时数列表

好的,我let date = Date()在 Swift 3 中,我需要获取最近的钟点列表。

假设当前日期是2017-02-08 15:24:56 +0000这样所需的列表

2017-02-08 15:30:00 +0000
2017-02-08 15:45:00 +0000
2017-02-08 16:00:00 +0000

我怎样才能得到那个日期列表?

编辑 1 2/8/2017 下午 5:54

这是我刚刚尝试做的事情,看起来它得到了我需要的结果。

    let currentDate = Date()
    var minute = Calendar.current.component(.minute, from: currentDate)

    if minute < 15 {
        minute = 15
    } else if minute < 30 {
        minute = 30
    } else if minute < 45 {
        minute = 45
    } else {
        minute = 0
    }

    var hourQuarterDate = Calendar.current.nextDate(after: currentDate, …
Run Code Online (Sandbox Code Playgroud)

formatting date nsdateformatter nsdatecomponents swift3

3
推荐指数
1
解决办法
1819
查看次数

Swift - Date from components is incorrect when printed. Date has 2 values?

I'm trying to store a time of day in a Date:

let calendar = NSCalendar.init(identifier: .gregorian)
let components = NSDateComponents()
components.hour = 7
components.minute = 0

var newDate = calendar?.date(from: components as DateComponents)!

print(newDate!)
Run Code Online (Sandbox Code Playgroud)

However, this yields some bizarre results when I try to print or otherwise use the value. Here's a Swift Playground of the results:

奇怪的日期结果

How can newDate be both 7:00AM and 11:56AM at the same time?

nscalendar nsdatecomponents swift

3
推荐指数
1
解决办法
1076
查看次数

DateComponentsFormatter 和 .nanosecond

DateComponentsFormatter因此,我在只需要格式化给定 的纳秒的情况下使用 a TimeInterval。问题是它不能按预期工作,或者我在这里混淆了一些我不知道的东西。

这里有 3 个快速测试来说明我所说的内容。

第一个场景

let fooFormatter = DateComponentsFormatter()

fooFormatter.allowedUnits = [.second, .nanosecond]
fooFormatter.unitsStyle = .full
fooFormatter.collapsesLargestUnit = true
fooFormatter.allowsFractionalUnits = true

print(fooFormatter.string(from: 42.42424242424242424242424242424242))
Run Code Online (Sandbox Code Playgroud)

输出Optional("42 seconds")

预期:因为这会折叠“最大单位”(在本例中为秒),所以预计会类似于(Optional("42.42424242424242424242424242424242 * 1e^9"))

场景二

let fooFormatter = DateComponentsFormatter()

fooFormatter.allowedUnits = [.second, .nanosecond]
fooFormatter.unitsStyle = .full
fooFormatter.allowsFractionalUnits = true

print(fooFormatter.string(from: 42.42))
Run Code Online (Sandbox Code Playgroud)

输出Optional("42 seconds")

预期:即使没有选择不崩溃.second,我的预期也接近(Optional("42 seconds 0.42 * 1e^9 nanoseconds"))

第三个场景

let fooFormatter = DateComponentsFormatter()
fooFormatter.allowedUnits = …
Run Code Online (Sandbox Code Playgroud)

time nsdateformatter nsdatecomponents swift nsdatecomponentsformatter

3
推荐指数
1
解决办法
907
查看次数

NSDate/NSDateComponent问题

假设我们是15/07/2010,我想获得前一周的第一天,也就是7月5日.我有以下方法,但它不起作用.似乎我的工作日属性有问题......

+ (NSDate *)getFirstDayOfPreviousWeek
{
// Get current date
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];

// Get today date at midnight
NSDateComponents *components = [cal components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSTimeZoneCalendarUnit) fromDate:now];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
NSInteger *dayNbr = [components weekday];
NSLog(@"week day:%@",dayNbr); // CRASH
NSDate *todayMidnight = [cal dateFromComponents:components];

// Get first day of previous week midnight
components = [[[NSDateComponents alloc] init] autorelease];
NSInteger …
Run Code Online (Sandbox Code Playgroud)

iphone nsdate nsdatecomponents

2
推荐指数
1
解决办法
6941
查看次数

如何在明天早上8点创建一个日期对象

我通常对此很好,但我遇到了这个问题NSDate.我需要NSDate明天早上8点(相对)设置一个对象.我该怎么做?什么是最简单的方法?

objective-c nsdate nsdatecomponents uilocalnotification

2
推荐指数
1
解决办法
2866
查看次数

找出最后被解雇的UILocalNotification

我正在开发一个使用UILocalNotifications的iOS应用程序.到目前为止一直很好,直到有人提出显示从用户所在的路线到他想去的地方的想法.

为此,我想我可能会得到最后一次激活的UILocalNotification(所有这些都将repeatInterval设置为每周或每天).

这是整个问题的来源,因为fireDate是我第一次安排它们:这是我的场景:

        UILocalNotification* ln = [[UILocalNotification alloc] init];
        ln.fireDate = date; // For instance 07/19/2014 10:00:00 (Saturday)
        ln.repeatInterval = NSDayCalendarUnit;            
        [[UIApplication sharedApplication] scheduleLocalNotification:ln];

        UILocalNotification* ln = [[UILocalNotification alloc] init];
        ln.fireDate = date; // For instance 07/17/2014 11:00:00 (Thursday)
        ln.repeatInterval = NSWeekCalendarUnit;            
        [[UIApplication sharedApplication] scheduleLocalNotification:ln];
Run Code Online (Sandbox Code Playgroud)

让我们想象一下:

下周四(2014年7月24日11:00:01)我想知道最后一次被解雇的UILocalNotification是第二个只在周四重复,第二天我想要第一个,因为它每天重复.

我试图按日期排序所有LocalNotifications但没有成功.

有没有人曾经遇到过类似于它的情况?

问候

date nsdate nsdatecomponents ios

2
推荐指数
1
解决办法
1106
查看次数

NSDateComponents返回错误的工作日

如果您将运行以下代码:

// January 16th 2015 10:20 AM in Amsterdam
var date = NSDate(timeIntervalSince1970: 1421400000)

var formatter = NSDateFormatter()
formatter.dateFormat = "dd-MMM"

let calendar = NSCalendar.currentCalendar()
calendar.firstWeekday = 2 // default when region == Netherlands

let units = NSCalendarUnit.YearCalendarUnit | NSCalendarUnit.MonthCalendarUnit
    | NSCalendarUnit.WeekOfMonthCalendarUnit | NSCalendarUnit.DayCalendarUnit
    | NSCalendarUnit.WeekdayCalendarUnit

// Loop days in January
for day in 1...14 {

    // select day in month
    var components = calendar.components(units, fromDate: date)
    components.day = day

    // get day and components
    let _date = calendar.dateFromComponents(components)!
    var …
Run Code Online (Sandbox Code Playgroud)

nsdatecomponents ios swift

2
推荐指数
1
解决办法
2789
查看次数

在Swift 2.0中将NSCalendarUnit与OR(管道)相结合时出错

我有一些在Swift 2.0中破解的代码:

let formatter = NSDateComponentsFormatter()
formatter.allowedUnits = NSCalendarUnit.Year
formatter.allowedUnits |= .Month
formatter.allowedUnits |= .WeekOfMonth 
formatter.allowedUnits |= .Day
formatter.allowedUnits |= .Hour
formatter.allowedUnits |= .Minute
Run Code Online (Sandbox Code Playgroud)

我收到了错误Binary operator '|=' cannot be applied to 'NSCalenderUnit' operands.

做这种事的新方法是什么?

nsdateformatter nscalendar nsdatecomponents swift swift2

2
推荐指数
1
解决办法
342
查看次数