相同的代码
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"d MMMM yyyy GG"];
//13th of December 1577
NSDate * aDate = [NSDate dateWithTimeIntervalSince1970:-12371067248];
NSString * formattedDate = [dateFormatter stringFromDate:aDate];
Run Code Online (Sandbox Code Playgroud)
Mac OS X目标正确显示formattedDate
1577年12月13日
iOS 5.0目标显示
1577年12月23日
好像朱利安 - >格里高利过渡被窃听了
任何想法如何克服?
我正试图milliseconds从NSCalendarUnit. 我能够得到nanosecond,但是数字太多了。我想要的只是 2 个数字。这是我的代码:
var startDate = NSDate()
let dateComponents = NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitNanosecond, fromDate: startDate, toDate: NSDate(), options: nil)
let strFraction = String(format: "%02d", dateComponents.nanosecond)
timeLabel.text = "\(strFraction)"
Run Code Online (Sandbox Code Playgroud)
如何从 NSCalendarUnit 获取毫秒?
当我计算一天的开始时间时,会获得一个小时的时差。为什么未针对GMT + 1进行更正?
let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
cal.timeZone = NSTimeZone.localTimeZone()
let startOfToday = cal.startOfDayForDate(NSDate())
print(NSTimeZone.localTimeZone())
print(startOfToday)
Run Code Online (Sandbox Code Playgroud)
输出:
"Local Time Zone (Europe/Amsterdam (GMT+1) offset 3600)\n"
"2016-01-14 23:00:00 +0000\n"
Run Code Online (Sandbox Code Playgroud) 我想在没有秒或毫秒的CoreData存储中保留日期/时间.(我正在进行一些处理以使时间过去,并且流浪的秒/毫秒成为一个猴子扳手.)这很容易放下秒:
NSDate *now = [NSDate date];
NSDateComponents *time = [[NSCalendar currentCalendar]
components:NSHourCalendarUnit | NSMinuteCalendarUnit
| NSSecondCalendarUnit fromDate:now];
NSDate *nowMinus = [now addTimeInterval:-time.second];
// e.g. 29 Aug 10 4:43:05 -> 29 Aug 10 4:43:00
Run Code Online (Sandbox Code Playgroud)
这很好地将秒数归零,但我找不到一个NSMillisecondCalendarUnit我可以用来将毫秒归零,我需要.有任何想法吗?谢谢.
我正在尝试创建一个30天的阵列,考虑到夏令时,闰年等.我目前有一个发电机,它可以产生一系列天数,但它没有考虑到特殊的时间变化和年份,月变化.这是我目前的代码:
NSMutableArray* dates = [[NSMutableArray alloc] init];
int numberOfDays=30;
NSDate *startDate=[NSDate date];
NSDate *tempDate=[startDate copy];
for (int i=0;i<numberOfDays;i++) {
NSLog(@"%@",tempDate.description);
tempDate=[tempDate dateByAddingTimeInterval:(60*60*24)];
[dates addObject:tempDate.description];
}
NSLog(@"%@",dates);
Run Code Online (Sandbox Code Playgroud)
什么是创建生成器循环日历以从今天的日期开始检索接下来30天的最佳方法,阵列应包括今天的日期和接下来的29天.我当前的代码就像我说的那样,但它并不完全准确.谢谢
在Mac OS X上,如果您转到系统首选项>日期和时间>打开语言和文本>在顶部单击格式>然后使用日历下拉菜单,可以选择科英日历.如何在Mac应用程序中显示此日历?
如果我尝试使用以下内容从应用程序内的任何位置任意设置时区:
[[NSCalendar currentCalendar] setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"EST"]];
NSTimeZone *tz = [[NSCalendar currentCalendar] timeZone];
NSLog(@"%@", tz);
Run Code Online (Sandbox Code Playgroud)
日志语句的结果是:
America/Los_Angeles(PDT)偏移-25200(日光)
(这是我当地的时区,即[NSTimeZone systemTimeZone])
但是,NSCalendar上类别中功能相似的代码可以正常工作:
[self setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"EST"]];
NSTimeZone *tz = [self timeZone];
NSLog(@"%@", tz);
Run Code Online (Sandbox Code Playgroud)
和日志产量:
America/New_York(EDT)抵消-14400(日光)
如何为[NSCalendar currentCalendar]设置时区?这种行为违反直觉.
我遇到了一个陌生人的问题.NSDateFormatter无法解析1988-04-10到iOS 6中的NSDate实例.但它适用于iOS 4和iOS 5.这是一个错误吗?
以下是代码.这很简单.
NSArray *values = @[@"1988-04-09", @"1988-04-10", @"1988-04-11"];
NSDateFormatter* df = [[NSDateFormatter alloc] init];
NSLocale *posixLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[df setLocale:posixLocale];
[df setDateFormat:@"yyyy'-'MM'-'dd"];
NSLog(@"timeZone:%@, locale:%@", df.timeZone, df.locale.localeIdentifier);
NSDate* date = nil;
for (NSString *value in values) {
date = [df dateFromString:value];
NSLog(@"The date is %@", date);
}
[posixLocale release];
[df release];
Run Code Online (Sandbox Code Playgroud)
以下是iOS 4和iOS 5中的结果.
2013-03-19 09:55:32.249 SampleProject[1844:f803] timeZone:Asia/Shanghai (CST (China)) offset 28800, locale:en_US_POSIX
2013-03-19 09:55:32.250 SampleProject[1844:f803] The date is 1988-04-08 16:00:00 +0000
2013-03-19 09:55:32.250 …Run Code Online (Sandbox Code Playgroud) 我们在日历中有这些方便的功能:
let calendar = NSCalendar.currentCalendar()
calendar.isDateInToday(date)
calendar.isDateInTomorrow(date)
Run Code Online (Sandbox Code Playgroud)
但我错过了这两个:
calendar.isDateInNextWeek(日期)
calendar.isDateInNextMonth(date)
正如@Rob 所提到的,意思是:“在即将到来的星期日开始的日历周中,经过下一个星期六”
我很难弄清楚如何以涵盖所有极端情况的稳健方式实现这些功能。
有人可以提供助手吗?
我不知道如何使用NSDate或NSCalendar在这种情况下.
帮我解决这个问题.
nscalendar ×10
nsdate ×8
ios ×5
objective-c ×4
swift ×3
cocoa ×2
iphone ×2
macos ×2
xcode ×2
cocoa-touch ×1
milliseconds ×1
nstimezone ×1