我正在寻找一个+ autoupdatingCurrentCalendar可以用来代替的例子+ currentCalendar.更具体地说,当日历改变时,基于日历的值自动改变.我是否需要绑定此类或类似的东西?
文档说明:
请注意,如果您根据日历或相关信息缓存值,那么这些缓存当然不会通过更新日历对象自动更新.
提前致谢
我使用以下NSCalendar方法获取日历年中的天数:
NSRange range = [gregorian rangeOfUnit:NSDayCalendarUnit
inUnit:NSYearCalendarUnit
forDate:date];
Run Code Online (Sandbox Code Playgroud)
我期望range.length返回值类型NSRange为365或366(天).
但是,此代码返回range.length的31对的日期值2005-06-06.
我的代码出了什么问题?
这是完整的代码段:
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
[subArray enumerateObjectsUsingBlock:
^(NSDate *date, NSUInteger idx, BOOL *stop) {
NSUInteger numberOfDays = [gregorian ordinalityOfUnit:NSDayCalendarUnit
inUnit:NSYearCalendarUnit forDate:date];
}];
Run Code Online (Sandbox Code Playgroud) 我有一个方法的子集,需要获得当月的第一天.
NSDate *today = [NSDate date]; // returns correctly 28 february 2013
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = 1;
NSDate *dayOneInCurrentMonth = [gregorian dateByAddingComponents:components toDate:today options:0];
Run Code Online (Sandbox Code Playgroud)
dayOneInCurrentMonth然后打印出来2013-03-01 09:53:49 +0000,下个月的第一天.
我如何获得当月的第一天?
在"邮件,通讯录,日历"部分的系统设置中,用户可以将任何工作日设置为一周的第一天.[[NSCalendar currentCalendar] firstWeekday]不返回此值但返回当前区域设置的值.Apple的日历应用程序响应用户首选项的变化.
如何获得用户个性化的第一天?
我有以下Swift(Swift 3)函数来生成一个Date带日期components(DateComponents)的date().
func makeDate(year: Int, month: Int, day: Int, hr: Int, min: Int, sec: Int) -> NSDate {
let calendar = NSCalendar(calendarIdentifier: .gregorian)!
let components = NSDateComponents()
components.year = year
components.month = month
components.day = day
components.hour = hr
components.minute = min
components.second = sec
let date = calendar.date(from: components as DateComponents)
return date! as NSDate
}
Run Code Online (Sandbox Code Playgroud)
如果我使用它,它将返回GMT日期.
override func viewDidLoad() {
super.viewDidLoad()
let d = makeDate(year: 2017, month: 1, day: 8, hr: 22, min: 16, …Run Code Online (Sandbox Code Playgroud) 我看过了它的timeIntervalSinceReferenceDate功能NSDate.我可以使用此功能将间隔存储到磁盘,然后将其返回到NSDate与原始值相同的值吗?我担心机器之间的参考或间隔会有所不同,而另一台计算机上的参考或间隔会有所不同?
我正试图显示伊斯兰当前的day名字,month名字.我使用NSLocale设置区域ar_SA,但它只是将日期和月份名称从英语翻译成阿拉伯语.
输出我用阿拉伯语:
?? ????? ? ?????? ? ???? 这是英文的 Sunday, October 28, 2012
我想要的输出:
Al-Ahad, Dul-Hijja 12, 1433
片段:
NSDateFormatter *islamicFormatter = [[NSDateFormatter alloc]init];
[islamicFormatter setDateStyle:NSDateFormatterFullStyle];
[islamicFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"ar_SA"]];
NSString *islamicDateString = [islamicFormatter stringFromDate:islamicDate];
NSLog(@"%@",islamicDateString);
Run Code Online (Sandbox Code Playgroud)
我希望你们这些都很清楚.
let dateComponents = calendar.components(.DayCalendarUnit | .MonthCalendarUnit | .YearCalendarUnit, fromDate: date)
Run Code Online (Sandbox Code Playgroud)
我收到了错误:
'DayCalendarUnit' was deprecated in iOS version 8.0: Use NSCalendarUnitDay instead
但是,当我用Apple建议的名称替换这些名称时
let dateComponents = calendar.components(NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear, fromDate: date)
Run Code Online (Sandbox Code Playgroud)
我收到错误:
Use of unresolved identifier 'NSCalendarUnitDay'
Run Code Online (Sandbox Code Playgroud) 我希望通过我的应用程序保存本地日历的条目.我有所有的条目详细信息,如姓名,时间/日期,地点等.有什么我可以开箱即用的反应原生.如果没有可用于此功能的现有插件?
我有格式的数据
时间:13:52,10:30,11:48
等等
我想绕到最近的一小时.
比如13:52 - > 14:00,10:30 - > 11:00和11:48 - > 12:00.
我怎么能用NSDate做到这一点?
nscalendar ×10
nsdate ×5
cocoa ×4
ios ×4
objective-c ×3
cocoa-touch ×2
android ×1
calendar ×1
iphone ×1
react-native ×1
swift ×1
swift3 ×1
xcode ×1