Tag: nsdateformatter

从iOS sdk中获取date

在我的应用程序中,想从date开始(即Sunday , Monday等)。 我的代码如下: NSDate *currentDate = [NSDate date]; NSCalendar *calendar = [NSCalendar currentCalendar]; //ask for current week NSDateComponents *comps = [[NSDateComponents alloc] init]; comps=[calendar components:NSWeekCalendarUnit|NSYearCalendarUnit fromDate:currentDate]; //create date on week start NSDate* weekstart=[calendar dateFromComponents:comps]; //add 7 days NSMutableArray* week=[NSMutableArray arrayWithCapacity:7]; for (int i=1; i<=7; i++) { NSDateComponents *compsToAdd = [[NSDateComponents alloc] init]; compsToAdd.day=i; NSDate […]

在swift中使用NSDateFormatter时,我得到零

这是我的代码: println(myStringDate) // 2015-02-26T17:46:34.000Z let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "YYYY-MM-DDTHH:mm:ss.sssZ" let myDate = dateFormatter.dateFromString(myStringDate) println(myDate) // nil 为什么零? 我究竟做错了什么 ?

格林尼治标准时间到本地时间转换夏时制改变

从服务器我收到格林尼治标准时间结构(用户定义的结构),使用,我想将其转换为本地时间,我已经完成填充NSDatecomponent收到的结构,然后我已经使用dateformatter获取date它,除了一个案件,一切正常。 如果GMT时间在11月3日(美国夏令时改变)之后格式化器产生1小时时差。 例如:如果预计时间是11月3日下午4点,格林尼治标准时间从格林威治标准时间转换到当地时间为11月3日下午3点。 任何想法如何避免它。 编辑: // Selected Dates NSDateComponents *sel_date = [[NSDateComponents alloc]init]; sel_date.second = sch_detail.sel_dates.seconds; sel_date.minute = sch_detail.sel_dates.mins; sel_date.hour = sch_detail.sel_dates.hours; sel_date.day = sch_detail.sel_dates.date; sel_date.month = sch_detail.sel_dates.month; sel_date.year = sch_detail.sel_dates.year; sel_date.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; // Get the Date format. NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; // Start_date formatter. NSDateFormatter *dateFormatter = […]

将string转换为自定义格式的NSDate

可能重复: NSString到NSDate iPhone:如何将“yyyyMMddThhmmss”格式string转换为NSDate? 如何将此格式转换为NSDate? 我的stringdate格式是: yyyy-m-d'T'H:m:ss (间隔之间没有空格)。 更新: 2012-11-06T12:17:22.947 would be the string that I'd like to convert to NSDate

从NSDateFormatterFullStylestring中删除一年

我已经search了这个问题,没有find一个决定性的和优雅的解决scheme。 是否有改变NSDateFormatterFullStyleyear抑制年份信息。 我正在使用的代码: NSString *dateDescription; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; [dateFormatter setTimeZone:gmt]; [dateFormatter setDateStyle:NSDateFormatterFullStyle]; dateDescription = [dateFormatter stringFromDate:date]; NSLog(@"%@", dateDescription); 结果:2013年3月13日星期三 问候,马科斯

一个错误? NSDateFormatter dateFromString仅为此date返回null

NSString *string = @"1990-04-15"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd"]; return [dateFormatter dateFromString:string]; 这总是返回null,当“string”被任何其他datestring改变时,它的function正常,这个date(1990-04-15)如此特别吗? Xcode 5,IOS 7,iPhone应用程序 整个方法和上下文: 在“Tools.m”中: +(NSDate *)stringToDate:(NSString *)string{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd"]; return [dateFormatter dateFromString:string]; } 和这个方法叫做: NSDate *date = [Tools stringToDate:@"1990-04-15"]; NSLog(@"—- the date:%@",date);

如何从当前date迅速获得下一个10天

我有dateformatstring这样的“2015-03-09”。我如何得到下一个10天date从当前date?任何帮助将不胜感激。提前感谢

如何将服务器响应date转换为本地时区?

在API调用响应中,我得到不同时区的date。 我想将其转换为用户的本地时区,用户可以从ios时区列表中select任何时区。 这都是本地的。 我们永远不会发送选定的用户时区到服务器。 同时使任何API调用可以说我正在select器中创build一个事件与选定的开始date。 我应该发送全球时区不与用户select的时区,因为它是本地的应用程序。 目前我正在改变select的defaultTimeZone,以便我得到适当的当前date等。并使用setTimeZone NSDateFormatter … [NSTimeZone timeZoneWithName … 这是一个更好的方法来改变应用程序的所有本地的defaultTimeZone? 在将NSdate发送到服务器时,我应该如何将NSdate转换回全局date?

将NSDate设置为24 – 12小时xcode格式的“TimePicker”

我正在使用一些NSDate对象,而且,我用我的iPhone 24小时的格式,所以当我testing我的应用程序,一切都很顺利,但是,我的一个朋友尝试了他的iPhone上的应用程序,但他用了12小时格式,经过一些研究,我发现问题是date,我不知道为什么我使用这个设置dateselect器和时间select器,所以我从数据库中读取一些值(是的,我已经检查了值,它们是正确的,这不是一个价值问题),所以所有这些数据被格式化,并作为一个NSDate放在一起,然后,我设置dateselect器和时间select器与我刚刚创build的date NSDateFormatter *currentDate = [[[NSDateFormatter alloc] init] autorelease]; [currentDate setDateFormat:@"yyyy-MM-dd HH:mm"]; NSDate * startDate =[currentDate dateFromString:[NSString stringWithFormat:@"%d-%d-%d %d:%d",yearVal,monthVal,dayVal,hourVal,minVal]]; [datePicker setDate:startDate]; [timePicker setDate:startDate]; 所以,yearVal,monthVal,dayVal,hourVal,minVal都是NSIntegers …事情就是,如果你的iPhone / iPod在你的时钟设置中使用了24小时,那么它是完美的,但是如果你使用12小时制,它崩溃并显示此错误消息 > *** Assertion failure in -[UIDatePickerView _updateBitsForDate:forced:andReload:animateIfNeeded:], /SourceCache/UIKit/UIKit-1914.85/UIDatePicker.m:1200 2012-06-29 > 01:19:15.753 MyApp[1215:707] *** Terminating app due to uncaught > exception 'NSInternalInconsistencyException', reason: 'Invalid > parameter not satisfying: date' […]

NSCalendardate错误

我试图使用NSCalendar和NSIslamicCalendar标识符。 但是一天结果不好,她是我的代码: NSCalendar *calandar = [[NSCalendar alloc]initWithCalendarIdentifier:NSIslamicCalendar]; NSDateComponents *components = [calandar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:[NSDate date]]; NSInteger theDay = [components day]; NSInteger theMonth = [components month]; NSInteger theYear = [components year]; [components setDay:theDay]; [components setMonth:theMonth]; [components setYear:theYear]; NSDate *thisDate = [calandar dateFromComponents:components]; NSLog(@"year=%i month=%i day=%i",theYear,theMonth,theDay); NSLog(@"thisdate = %@", [thisDate description]); NSString *strDate = […]