标签: nscalendar

NSCalendar dateFromComponents:GMT时区而不是systemTimeZone

这段代码

NSCalendar *calendar =[NSCalendar currentCalendar];
[gregorian setTimeZone:tz];

NSLog(@"%@", [gregorian timeZone]);

NSDateComponents *comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:[NSDate date]];

[comp setDay:info.day];
[comp setMonth:info.month];
[comp setYear:info.year];
[comp setHour:info.hour];
[comp setMinute:info.minute];
[comp setSecond:info.second];
[comp setTimeZone:tz];  

NSLog(@"%@", [comp timeZone]);
NSLog(@"%@", [gregorian dateFromComponents:comp]);
Run Code Online (Sandbox Code Playgroud)

在控制台中显示以下内容:

Europe/Moscow (MSKS) offset 14400 (Daylight)

Europe/Moscow (MSKS) offset 14400 (Daylight)

2011-08-31 20:00:00 +0000
Run Code Online (Sandbox Code Playgroud)

因此,正确指定了日历和组件的时区,但[gregorian dateFromComponents:comp]返回带有错误时区(GMT)的NSDate值.

我需要纠正什么才能获得合适的时区?

iphone nstimezone nscalendar ios

6
推荐指数
1
解决办法
8291
查看次数

什么是NSEraCalendarUnit?

今天我正在玩NSCalendar和NSDateComponents,我看到了像NSDayCalendarUnit和NSEraCalendarUnit这样的常量.如果今天是12月4日,则NSDayCalendarUnit返回4.NSEraCalendarUnit返回发布时显然为1的时代.有人在乎解释什么是时代吗?

cocoa objective-c nsdate nscalendar nsdatecomponents

6
推荐指数
1
解决办法
873
查看次数

存储绝对NSDate,而不是相对于时区

我正在创建一个iOS应用程序来跟踪出勤率.每个出勤条目存储在具有状态属性(例如,存在,不存在)的对象中,并且NSDate被称为属性的属性date表示该出勤记录被拍摄的日期.当我选择特定日期(使用UIDatePickerView或类似)时,我希望该日期的所有出勤记录(对象)都显示在表格视图中.

虽然原则上听起来很简单,但我遇到了与时区有关的问题.我知道NSDates的存储与时区无关(即它们相对于UTC/GMT +0000存储).这意味着,如果我在悉尼并参加,例如,2012年11月4日星期日,因为日期存储为独立的时区,如果我将我的iPhone/iPad带到不同的时区(如旧金山)所有出席记录会在一天之前转移,在这种情况下会转移到2012年11月3日星期六,因为那是当地时间(实际上是第二天,悉尼当地时间)出席的时刻.

我不希望这种情况发生 - 我希望日期是绝对的.换句话说,如果出席会议是在2012年11月4日星期日举行,那么无论在世界的哪个地方(以及无论哪个时区),我都需要留在那个日期.正如您所看到的,这与日历应用程序形成鲜明对比,在日历应用程序中,预约的时间根据时区而变化是可取的.

任何关于更好地解决这个问题的方法的建议都将受到赞赏.请记住,我选择显示的日期使用a 以时区独立格式UIDatePickerView返回当前NSDate,所以我还需要一种方法来进行简单的比较(最好是NSPredicate因为考勤对象存储在Core Data中)获取该特定日期的所有出勤对象.

objective-c nsdate nstimezone nscalendar ios

6
推荐指数
1
解决办法
2365
查看次数

NSCalendar,为什么设置第一个周末不影响计算结果?

我需要计算给定日期的工作日,但是,根据日历,一周可以在星期一加星,在星期日看星期日

所以我想设置它,从周一开始,使用

[[NSCalendar currentCalendar] setFirstWeekday:2];
Run Code Online (Sandbox Code Playgroud)

但是,计算结果是一样的

{
    [[NSCalendar currentCalendar] setFirstWeekday:1];
    NSDateComponents *weekdayComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:date];
    NSInteger weekday = [weekdayComponents weekday] - 1;
    NSLog(@"%d", weekday);
}
{
    [[NSCalendar currentCalendar] setFirstWeekday:2];
    NSDateComponents *weekdayComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:date];
    NSInteger weekday = [weekdayComponents weekday] - 1;
    NSLog(@"%d", weekday);
}
Run Code Online (Sandbox Code Playgroud)

返回相同的数字,但为什么?

cocoa nsdate nscalendar ios

6
推荐指数
2
解决办法
6799
查看次数

检测两个日期是否在同一日历日内

我的应用程序显示消息列表,每个消息都有一个创建日期.

我不想只显示日期,而是根据创建日期过去的时间显示不同的格式.

例如,这是我从日期创建日期字符串的方式:

NSDate *date = someMessage.creationDate;
NSTimeInterval timePassed = [[NSDate date] timeIntervalSinceDate:date];

if (timePassed < 60 ) { // less then one minute
  return @"now"
} else if (timePassed < 3600) { // less then one hour
  NSInteger minutes = (NSInteger) floor(timePassed / 60.0);
  return [NSString stringWithFormat:@"%d minutes ago", minutes];
}
Run Code Online (Sandbox Code Playgroud)

所以现在我想添加以下案例:

else if ("timePassed < 24 hours ago and within the same calendar day")
   // do something
} else if ("timePassed > 24 hours, or within previous calendar …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c nsdate nscalendar ios

6
推荐指数
2
解决办法
2541
查看次数

Objective-C中间点两次

我有两个NSDate格式为"h:mm a"作为时间(即早上6点和晚上8点).

我试图找出这两次中间点的时间.

对于上面的示例,上午6:00到晚上8:00之间的中点是下午1:00.

但我不知道如何在Objective-C中为iPhone sdk做到这一点.

任何帮助或建议或代码将不胜感激.

iphone objective-c nsdate nscalendar ios4

5
推荐指数
1
解决办法
1231
查看次数

使用NSCalendar检索月份的第一天

我执行以下代码片段:

NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]]; 
[comp setDay:1]; 

NSDate *firstDayOfMonthDate = [gregorian dateFromComponents:comp];
Run Code Online (Sandbox Code Playgroud)

firstDayOfMonthDate的值是= 30-4-2011但是删除是1-5-2011,即我想要任何指定日期的第一个月份日期.但它给了我上个月的最后日期.

提前致谢.

iphone objective-c nsdate nscalendar

5
推荐指数
1
解决办法
3207
查看次数

一周的开始日存储在NSLocale的哪个地方?

我住在荷兰.我们荷兰人喜欢将周一视为本周的开始.我知道美国人喜欢将星期日视为本周的开始.傻美国人;)

如果我想向全球用户提供周视图,我可以从NSLocale获得一周中首选的开始日期,还是设置面板是唯一可行的方法?

干杯,EP.

objective-c nsdate nscalendar nsdatecomponents eventkit

5
推荐指数
1
解决办法
1871
查看次数

将currentCalendar()Date转换为NSCalendarIdentifierChinese

我已经写了这个函数,以便在过去的100年中在中国历法上获得一年的第一天.从循环返回的一些值是正确的.我已通过此链接验证了中国新年的日期http://www.fengshuimiracle.com/154/how-work-out-your-animal-sign-and-chinese-year .是否有可以引用的农历,或者我在代码中遗漏了一些东西.

 override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
           loopthrough()

}

func returnDateForMonth(month:NSInteger, year:NSInteger, day:NSInteger)->NSDate{
    let comp = NSDateComponents()
    comp.month = month
    comp.year = year
    comp.day = day

    let chCal = NSCalendar(calendarIdentifier: NSCalendarIdentifierChinese)
    return chCal!.dateFromComponents(comp)!
}

func showDate(getDate:NSDate)->String{
    let date = getDate
    //let calendar = NSCalendar.currentCalendar()
    let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierChinese)
    let dateComponents = calendar!.components([NSCalendarUnit.Year], fromDate: date)
    let firstDay = returnDateForMonth(dateComponents.month, year: dateComponents.year, day: 1)
    let dateFormatter = …
Run Code Online (Sandbox Code Playgroud)

nscalendar swift swift2

5
推荐指数
1
解决办法
1085
查看次数

Swift 4:设置不同的日期和时间

我知道如何获得当地的日期和时间,但我想要做的是从不同的地方获取日期和时间.例如,我想知道纽约的时间和日期.我该如何解决这个简单的问题?

这是我的本地日期和时间代码:

let date = NSDate()
let calendar = Calendar.current

let components = calendar.dateComponents([.hour, .minute, .month, .year, .day, .second, .weekOfMonth], from: date as Date)

let currentDate = calendar.date(from: components) 
Run Code Online (Sandbox Code Playgroud)

我在这里搜索了一下,但我找不到我想要的东西,而且我还在寻找日期图书馆.如果您知道要重定向我的任何来源或样本,我真的很感激.

nsdate nscalendar nsdatecomponents ios swift

5
推荐指数
2
解决办法
6157
查看次数