我有一个日期,我需要拆分一些组件.例如
let components = NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitHour
let date = calendar.components(components, fromDate: aDate, toDate: NSDate(), options: nil)
var dateToPrint = "\(date.day) days \(date.hour) hours"
Run Code Online (Sandbox Code Playgroud)
dateToPrint将是从aDate到现在的天数和小时数.但如果我想要几周而不是几天
let components = NSCalendarUnit.CalendarUnitWeek | NSCalendarUnit.CalendarUnitHour
let date = calendar.components(components, fromDate: aDate, toDate: NSDate(), options: nil)
var dateToPrint = "\(date.week) weeks \(date.hour) hours"
Run Code Online (Sandbox Code Playgroud)
date.week不存在.那我怎么解决这个问题呢?
我试图只获取意味着小时、分钟、秒为零的日期。这就是我正在尝试的
extension Date {
var onlyDate: Date? {
get {
let calender = Calendar.current
var dateComponents = calender.dateComponents([.year, .month, .day], from: self)
dateComponents.timeZone = NSTimeZone.system
return calender.date(from: dateComponents)
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试时
date = Date().onlyDate
Run Code Online (Sandbox Code Playgroud)
它返回前一个日期。你可以查看屏幕截图,它给我的是 4 月 14 日,而应该是 15 日
以下代码显示了问题:从1435年的第一天开始整整一年不会导致1436年的第一天.
我缺少什么想法?
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:1];
[components setMonth:1];
[components setYear:1435];
NSCalendar *islamic = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];
NSDate *date = [islamic dateFromComponents:components];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setCalendar:islamic];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLog(@"%@", [dateFormatter stringFromDate:date]); // -> 01.01.1435
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setYear:1];
NSDate *dateWithOffset = [islamic dateByAddingComponents:offsetComponents toDate:date options:0];
NSLog(@"%@", [dateFormatter stringFromDate:dateWithOffset]);
// -> 30.12.1435 ... WHY NOT 01.01.1436 ????
Run Code Online (Sandbox Code Playgroud) 我注意到从iOS 8.0开始,所有NSCalendarUnits(例如NSDayCalendarUnit)都被正式弃用.
什么是替代品?
直到iOS 9我们写local notifications这样的
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
localNotification.alertBody = self.textField.text;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSCalendarUnitMinute;
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Run Code Online (Sandbox Code Playgroud)
我们在本地通知中repeatInterval,现在在WWDC2016Apple宣布User Notification其中包含
UNCalendarNotificationTrigger.
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES];
Run Code Online (Sandbox Code Playgroud)上面的代码会在每分钟后触发通知.但无法设定日期.
NSDateComponents* date = [[NSDateComponents alloc] init];
date.hour = 8;
date.minute = 30;
UNCalendarNotificationTrigger* triggerC = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:date repeats:YES];
Run Code Online (Sandbox Code Playgroud)
上面的代码可以设置,重复将在明天8:30触发,而不是在分钟后.
在iOS 10中User Notification我如何设置重复频率的日期时间就像我们可以设置的一样UILocalNotification?
我希望 …
我一直在使用dateByAddingComponents:toDate:options:并dateByAddingUnit:value:toDate:options:使用可选绑定来获取日期.像这样:
guard let startOfNextMonth = calendar.dateByAddingComponents(oneMonthComponent, toDate: startOfThisMonth, options: []) else {
return nil
}
Run Code Online (Sandbox Code Playgroud)
其中oneMonthComponent只是一个月份值设置为1的NSDateComponent.
当我读到它时,在文档中他们都说了如下:
如果日期超出接收器的定义范围或者无法执行计算,则返回nil.
而且我一直在想,究竟是什么时候造成的.如果我只是添加一个月的日期,有没有办法可以是零?这是因为在其他日历系统中存在一些差异,其中添加一些单位没有意义吗?
我已经四处寻找并且无法找到一个返回nil的实例.
是否有任何可以使上面的代码为零?添加组件可能为零的示例是什么?
当我使用NSDateComponents时,我怎么知道我将时间设置为Am或Pm?还是24小时格式?这是代码:
NSDate *morningEnd = [NSDate date];
NSDateComponents *components1 = [gregorian components:NSUIntegerMax fromDate:morningEnd];
[components1 setHour:11];
[components1 setMinute:59];
Run Code Online (Sandbox Code Playgroud)
我如何知道我是将其设置为上午11:59或晚上11:59?
我有一个方法的子集,需要获得当月的第一天.
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,下个月的第一天.
我如何获得当月的第一天?
我有以下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) 在我的应用程序中,我必须设置每周提醒,以便在一周后的同一天/时间发出警报.我一直在使用NSDateComponenents.week = 1并使用NSCalendar的dateByAddingComponents:toDate:options:API 将其添加到NSDate .似乎week现在在iOS7中被弃用,警告告诉我"使用weekOfMonth或weekOfYear,具体取决于你的意思".但我找不到关于其中任何一个含义的文档.
谁能解释两者之间的区别?在基本测试中,两者在添加到NSDate时返回相同的值,但我确信两者之间存在一些有意义的差异.
nsdatecomponents ×10
nsdate ×7
ios ×5
nscalendar ×5
objective-c ×4
swift ×3
cocoa ×2
cocoa-touch ×2
calendar ×1
ios8 ×1
iphone ×1
macos ×1
swift3 ×1
time ×1