nsdate

iPhone: NSDate convert GMT to local time

谁说胖子不能爱 提交于 2019-11-27 13:29:34
问题 I currently have an API call returning me a date/time in the format: 2010-10-10T07:54:01.878926 Can I assume this is GMT? I also converted this to an NSDate object. Is there a way to use the local iPhone time to recalculate the time? 回答1: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm"; NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; [dateFormatter setTimeZone:gmt]; NSString *timeStamp = [dateFormatter

get time between two times of the day

旧时模样 提交于 2019-11-27 13:22:31
I've already tried with NSDate but with no luck. I want the difference between for example 14:10 and 18:30. Hours and minutes. I Hope you can help me shouldn't be that complicated :) There's no need to calculate this by hand, take a look at NSCalendar . If you want to get the hours and minutes between two dates, use something like this: NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit; NSDateComponents *components = [gregorianCalendar components:unitFlags fromDate:firstDate

Human friendly date descriptions with NSDate on iOS

亡梦爱人 提交于 2019-11-27 13:07:46
问题 I want to display NSDates in a "human-friendly way", such as "last week", or "a few days ago". Something similar to Pretty Time for Java. What's the best way to do this, subclassing NSDateFormatter? Before I go and reinvent the wheel, is there already some library for this? 回答1: This is the solution in Swift 2: func formattedHumanReadable(date: NSDate) -> String { let formatter = NSDateFormatter() formatter.timeStyle = .NoStyle formatter.dateStyle = .ShortStyle formatter

Swift 3: Date vs NSDate?

♀尐吖头ヾ 提交于 2019-11-27 13:04:36
What is the difference between the NS and non NS classes? Particularly NSDate vs Date ? Does NS represent some type of wrapper around the core non NS functionality? Swift 3 introduced some new overlay value types for existing Foundation class types, such as Date for NSDate , Data for NSData and some more. The full list and details can be found in SE-0069 Mutability and Foundation Value Types Some of the reasons were Provide proper value semantics, let and var instead of mutable and immutable variants, more "Swifty" APIs. The new overlay types should provide all functionality that the

Convert ISO 8601 to NSDate

一曲冷凌霜 提交于 2019-11-27 12:40:25
I have a timestamp coming from server that looks like this: 2013-04-18T08:49:58.157+0000 I've tried removing the colons, I've tried all of these: Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset? Why NSDateFormatter can not parse date from ISO 8601 format Here is where I am at: + (NSDate *)dateUsingStringFromAPI:(NSString *)dateString { NSDateFormatter *dateFormatter; dateFormatter = [[NSDateFormatter alloc] init]; //@"yyyy-MM-dd'T'HH:mm:ss'Z'" - doesn't work //@"yyyy-MM-dd'T'HH:mm:ssZZZ" - doesn't work //@"yyyy-MM-dd'T'HH:mm:sss" - doesn't work

Relative string from NSDate

╄→гoц情女王★ 提交于 2019-11-27 12:29:18
Does anyone know of a library or something that will convert an NSDate into strings like the examples below? 1 hour ago yesterday last Thursday 2 days ago last month 6 months ago last year David Schaefgen NSDateFormatter will do a great deal of what is mentioned above by using setDoesRelativeDateFormatting: on an instance. From there, you can control the formatting using the date style and the time style to fine tune it to your needs. If that doesn't get you what you need then check out SORelativeDateTransformer , it is an NSValueTransformer subclass that attempts to provide the same behavior

iOS时间那点事--NSDate分类

我的未来我决定 提交于 2019-11-27 12:06:01
#iOS 时间那点事 ##NSDate分类(Catogery) ###分类 类,是面向对象语言的基本;类,是具有相同属性和行为的一类事物的概括,这是一种抽象;但是,事物总是多方面的,如此之多以至于我们每每看到深不见底的头文件就恐高,于是乎我们使用了继承,鉴于管理和使用的高额成本,我们创造了“抽象工厂模式”,Perfect!但我们怎么有时心里就是高兴不起来呢?因为有些时候可以不用继承,但是又没有其他办法,只好用继承,并且不得不承担使用继承的带来的后果。Objective-C就在这时来到了你的身边。[单继承!!??什么东西?(C++)] [还不是跟我们一样~(Java)]。关于单继承和多继承的问题,作为一介平民,不做过多讨论,用着用着就习惯了…… 全世界针对这个问题给出了同样的答案:接口(和Objective-C中的协议一样的东西)。这就结束了吗??!!类还是深不见底啊!那就彻底一次吧,不要磨磨唧唧的了,一个类可以分开定义和实现,放到多个的文件里,这样把基本的东西留在原来的文件中,其他比较特定的东西放到其他文件里面,搞个合理的命名规则,就叫“分类”吧。 分类,通过分离特定功能的代码,解决大型代码框架的维护的问题。任何东西的出现都是有其一定的原因的,就像面向对象语言的出现一样,我们现在只不过是在不断地完善面向对象语言而已,可见前方的路还很远。 NSDate是一个时间类

ios时间那点事--NSCalendar NSDateComponents

混江龙づ霸主 提交于 2019-11-27 12:05:51
#iOS时间那点事 ##NSCalendar + NSDateComponents 历法能使人类确定每一日再无限的时间中的确切位置并记录历史。 日历,历法,一般历法都是遵循固定的规则的,具有周期性。日历都是已知的或可预测的。 任何一种具体的历法,首先必须明确规定起始点,即开始计算的年代,这叫“纪元”;以及规定一年的开端,这叫“岁首”。此外,还要规定每年所含的日数,如何划分月份,每月有多少天等等。 NSCalendar对世界上现存的常用的历法进行了封装,既提供了不同历法的时间信息,又支持日历的计算。 NSDateFomatter 表示的时间默认以公历(即阳历)为参考,可以通过设置calendar属性变量获得特定历法下的时间表示。 NSDate 是独立与任何历法的,它只是时间相对于某个时间点的时间差; NSDate 是进行日历计算的基础。 NSDateComponents将时间表示成适合人类阅读和使用的方式,通过NSDateComponents可以快速而简单地获取某个时间点对应的“年”,“月”,“日”,“时”,“分”,“秒”,“周”等信息。当然一旦涉及了年月日时分秒就要和某个历法绑定,因此NSDateComponents必须和NSCalendar一起使用,默认为公历。 NSDateComponents除了像上面说的表示一个时间点外,还可以表示时间段,例如:两周,三个月,20年,7天

iOS时间那点事--NSDateFormatter

天大地大妈咪最大 提交于 2019-11-27 12:05:25
#iOS时间那点事 ##NSDateFormatter NSDateFormatter是NSFormatter的子类,另,NSFormatter的用途是“将数据在字符串与特定类型的对象之间转换”,目前NSFormatter只有两个子类NSNumberFormatter和NSDateFormatter。 尽管NSDateFormatter提供了许多已定义好的时间格式,但是开发中开发人员更加喜欢自定义时间格式。 将时间字符串转换到NSDate对象,一般都是使用"年月日 时分秒",数据库中的date类型基本上也是这样的时间类型。 格式一般为:yyyy-MM-dd HH:mm:ss。 注意:yyyy是小写的;大写的YYYY的意思有些不同——“将这一年中第一周的周日当作今年的第一天”,因此有时结果和yyyy相同,有时就会不同。 将NSDate对象转换成特定格式的字符串。 转换后的字符串会根据设备的“区域格式”,显示特定语言的结果。假如程序需要保证不同语言环境下显示一致,请注意这方面的问题,使用其他代替方法! 一言足以明志: <!-- lang: cpp --> NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"'公元前/后:'G '年份:'u'=

iOS时间那点事--NSDate

早过忘川 提交于 2019-11-27 12:05:11
#iOS时间那点事 ##NSDate NSDate对象用来表示一个具体的时间点。 NSDate是一个类簇,我们所使用的NSDate对象,都是NSDate的私有子类的实体。 NSDate存储的是GMT时间,使用的时候会根据 当前应用 指定的 时区 进行时间上的增减,以供计算或显示。 可以快速地获取的时间点有: now (当前时间点) 相对于1 January 2001, GMT的时间点 相对于1970的时间点 distantFuture (不可达到的未来的某个时间点) distantPast (不可达到的过去的某个时间点 根据 http://www.gnustep.org/实现的NSDate的版本: <!-- lang: cpp --> @interface NSDate : NSObject <NSCoding, NSCopying> { NSTimeInterval _secondsSinceRef; } …… - (id) initWithTimeInterval:(NSTimeInterval) secsToBeAdded sinceDate:(NSDate *) anotherDate; 相对于已知的某个时间点 - (id) initWithTimeIntervalSinceNow:(NSTimeInterval) secsToBeAdded; 相对于当前时间 - (id)