我必须从Date发送DDMMYYY日期以与API通信.
NSDateComponents*dateComponents; NSCalendar*格里高利; NSDate*日期;
gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
dateComponents = [[NSDateComponents alloc] init];
self.date = [gregorian dateByAddingComponents:dateComponents toDate:[NSDate date] options:0];
Run Code Online (Sandbox Code Playgroud)
我必须使用[dateComponents日]等等吗?是否有一些方法可以帮助我像PHP一样执行该任务?
我使用下面的Utility方法显示经过的秒,分钟,小时等.
例如,1小时前
我有一个朋友在海外测试我们的iPhone应用程序,每次他们提交一个项目时,该方法总是返回"1小时前",因为新西兰提前2小时,服务器托管在澳大利亚,显然落后2小时.
直到我们的SQL Server赶上; 当项目日期开始正确计算时.
只是想知道如何处理时区差异并修改我的效用方法以全局工作?请随意帮助修改方法,这样就不需要那么多行代码(如果你感到非常无聊 - 那么慷慨).
提前致谢.
+ (NSString *)elapsedTime:(NSDate *)date {
NSUInteger desiredComponents = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *elapsedTimeUnits = [[NSCalendar currentCalendar] components:desiredComponents
fromDate:date
toDate:[NSDate date]
options:0];
// Format to be used to generate string to display.
NSString *scannedFormat = @"%d %@ ago";
NSInteger number = 0;
NSString *unit;
if ([elapsedTimeUnits year] > 0) {
number = [elapsedTimeUnits year];
unit = [NSString stringWithFormat:@"year"];
} else if …Run Code Online (Sandbox Code Playgroud) 我使用此方法将月份添加到日期
- (NSDate *)sameDateByAddingMonths:(NSInteger)addMonths {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents * components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:self];
[components setMonth:components.month + addMonths];
return [calendar dateFromComponents:components];
}
Run Code Online (Sandbox Code Playgroud)
但是当前一个月在自我NSDate,有更多的日子,比它在下个月的第一天跳跃,就像
6月31日=>自我是6月31日召集这个,将日期设定为1. 8月,自7月起30天
如何做到这一点?我认为这应该表现得"正确",并在月末剪辑
我想从当前日期开始一个月.
以下是我所拥有的,它正在回归:4027-09-24 16:59:00 +0000.currentDate是对的.这是怎么回事?
// Get todays date to set the monthly subscription expiration date
NSDate *currentDate = [NSDate date];
NSLog(@"Current Date = %@", currentDate);
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit fromDate:currentDate];
dateComponents.month = dateComponents.month + 1;
NSDate *currentDatePlus1Month = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:currentDate options:0];
NSLog(@"Date = %@", currentDatePlus1Month);
Run Code Online (Sandbox Code Playgroud) NSDateFormatter对生成相关日期有很好的支持,例如"今天","明天","昨天",这些都是当前语言支持的.一个很大的优点是所有这些都已经为您本地化 - 您不需要翻译字符串.
您可以使用以下命令打开此功能:
[dateFormatter setDoesRelativeDateFormatting: YES];
Run Code Online (Sandbox Code Playgroud)
不利的一面是,这似乎只适用于使用其中一种预定义格式的实例,例如:
[dateFormatter setDateStyle: NSDateFormatterShortStyle];
Run Code Online (Sandbox Code Playgroud)
如果您设置日期格式化程序以使用自定义格式,如下所示:
[dateFormatter setDateStyle: @"EEEE"];
Run Code Online (Sandbox Code Playgroud)
然后当你打电话:
[dateFormatter stringFromDate: date];
Run Code Online (Sandbox Code Playgroud)
......你会回来一个空字符串.
我希望能够在可能的情况下获得相关字符串,并在不支持时使用我自己的自定义格式.
我使用下面的代码来在特定时间安排本地通知.但通知是每分钟而不是一天后重复的.我是否错过了任何通知设置.我的时区是(亚洲/加尔各答(IST)偏移19800)并使用iPhone 4s.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
NSDate *now = [NSDate date];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:now];
[components setHour:12];
[components setMinute:00];
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.fireDate = [calendar dateFromComponents:components];
[notification setAlertBody:@"U got notification!!!"];
// notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的应用程序的用户默认值中存储和检索特定时间(因为它的重复事件日期没有相关性而被忽略),但我有这个奇怪的问题,其中小时是正确的但是分钟和秒不是
这是我的代码
var calendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)
var flags: NSCalendarUnit = .MinuteCalendarUnit | .HourCalendarUnit | .SecondCalendarUnit
var components = calendar?.components(flags, fromDate: NSUserDefaults.standardUserDefaults().valueForKey("breakfastTime") as NSDate!)
let test = calendar?.dateFromComponents(components!) as NSDate!
NSLog("Components: %@", components!)
NSLog("Date 1: %@", NSUserDefaults.standardUserDefaults().valueForKey("breakfastTime") as NSDate)
NSLog("Date 3: %@", test!)
Run Code Online (Sandbox Code Playgroud)
组件正确地输出时间
Components: <NSDateComponents: 0x17414a7c0>
Hour: 8
Minute: 30
Second: 0
Run Code Online (Sandbox Code Playgroud)
并且从用户默认值中正确检索日期
Date 1: 2015-01-28 07:30:00 +0000
(It's 7 rather than 8 but it makes sense, since it's stored at UTC)
Run Code Online (Sandbox Code Playgroud)
但是,使用dateFromComponents时会发生这种情况
Date 3: 0001-01-01 07:40:04 +0000
Run Code Online (Sandbox Code Playgroud)
我甚至不明白40和04来自哪里
func updateTime() {
var date = NSDate()
var calendar = NSCalendar.currentCalendar()
var components = calendar.components(.CalendarUnitSecond, fromDate: date)
var hour = components.hour
var minutes = components.minute
var seconds = components.second
counterLabel.text = "\(seconds)"
var myIndicator = counterLabel.text?.toInt()
if myIndicator! % 2 == 0 {
// do this
} else {
// do that
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何更改此代码,以便在counterlabel.text中显示1/10或1/100或1/1000秒.只是想不出来......谢谢!
我们可以使用下面的线获得日期的日期.
let day = cal.ordinalityOfUnit(.Day, inUnit: .Year, forDate: date)
Run Code Online (Sandbox Code Playgroud)
但是我们如何才能从一年中获取日期?
我正在尝试计算2个NSDates之间的工作日+工作日数,但我似乎找不到合适的解决方案.我现在可以找到两个NSDates之间的天数,如下所示:
func reloadData(){
let cal = NSCalendar.currentCalendar()
var daysInt = 0
let days = cal.components(.Day, fromDate: selectedDateTimePointTwo, toDate: selectedDateTimePointOne, options: [])
daysInt = days.day
workDaysLabel.text = "work days: \(daysInt)"
weekendDaysLabel.text = "weekend days: "
}
Run Code Online (Sandbox Code Playgroud)
谁能指出我正确的方向?
nscalendar ×10
nsdate ×8
ios ×6
swift ×4
objective-c ×3
calendar ×2
date ×2
cocoa-touch ×1
ios7 ×1
iphone ×1
macos ×1
swift3 ×1