nsdate

iPhone SDK NSString To NSDate

人盡茶涼 提交于 2019-11-30 03:04:07
问题 I got a string from parsing a XML file which looks like this: Fri, 09 Apr 2010 00:00:45 +0200 and the corresponding pattern should be this "EEE, dd MMM yyyy HH:mm:ss ZZ", but I get (null). This is my code: NSString *dateString = @"Fri, 09 Apr 2010 00:00:45 +0200"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZ"]; NSDate *date = [dateFormatter dateFromString:dateString]; NSLog(@"date:%@",date); // result date:(null)

Compare two dates [duplicate]

萝らか妹 提交于 2019-11-30 02:22:30
This question already has an answer here: How to compare two dates in Objective-C 14 answers How can I compare two different dates to find out which is the later date? For example, in date1 I will store one date after downloading some data, and in date2 , I will store the current date. Then I need to check which one is greater/later: something like if(date1>date2) . Something like: NSDate* timeNow = [NSDate date]; // If less than 30 seconds, do something if ([timeNow timeIntervalSinceDate:anEarlierTime] < 30.0f) { // Do something } PgmFreek you can use NSDate's compare: method:

get time and date by NSDate

与世无争的帅哥 提交于 2019-11-29 22:56:57
how can I retrieve the date and time from a NSDate. I want to see them separately. thanks Brodie NSDate *localDate = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; dateFormatter.dateFormat = @"MM/dd/yy"; NSString *dateString = [dateFormatter stringFromDate: localDate]; NSDateFormatter *timeFormatter = [[NSDateFormatter alloc]init]; timeFormatter.dateFormat = @"HH:mm:ss"; NSString *dateString = [timeFormatter stringFromDate: localDate]; There are many many different ways to represent the date and time so check NSDateFormatter for more info. i would go with using

How to add one month to an NSDate?

时间秒杀一切 提交于 2019-11-29 20:32:42
How To Add Month To NSDate Object? NSDate *someDate = [NSDate Date] + 30Days.....; TheEye You need to use NSDateComponents : NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; [dateComponents setMonth:1]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:originalDate options:0]; [dateComponents release]; // If ARC is not used, release the date components With iOS 8 and OS X 10.9 you can add NSCalendarUnits using NSCalendar : Objective-C NSCalendar *cal = [NSCalendar currentCalendar]; NSDate *someDate =

Swift - check if a timestamp is yesterday, today, tomorrow, or X days ago

邮差的信 提交于 2019-11-29 20:16:29
I'm trying to work out how to decide if a given timestamp occurs today, or +1 / -1 days. Essentially, I'd like to do something like this (Pseudocode) IF days_from_today(timestamp) == -1 RETURN 'Yesterday' ELSE IF days_from_today(timestamp) == 0 RETURN 'Today' ELSE IF days_from_today(timestamp) == 1 RETURN 'Tomorrow' ELSE IF days_from_today(timestamp) < 1 RETURN days_from_today(timestamp) + ' days ago' ELSE RETURN 'In ' + days_from_today(timestamp) + ' ago' Crucially though, it needs to be in Swift and I'm struggling with the NSDate / NSCalendar objects. I started with working out the time

How to add hours to an NSDate?

让人想犯罪 __ 提交于 2019-11-29 20:14:48
I have a date converted to double value and saved in database. Now, I want to compare if currentDate > myDataBaseDate + 8 hours i.e., I want to get 8 hours added to myDataBaseDate. I'm converting date into double values. So how do I get 8 hours later time from my database saved date. How do I compare if (currentDateTime > DateFromdatabaseValue + DateByAdding8HoursInDataBase) Tom Jefferys I'm not entirely sure what you are trying to do, but you can create an NSDate object by adding time in seconds on to another NSDate using: - (id)dateByAddingTimeInterval:(NSTimeInterval)seconds // eg. to add 8

Converting a Gregorian date to Julian Day Count in Objective C

做~自己de王妃 提交于 2019-11-29 20:04:09
问题 I need Objective C method for converting Gregorian date to Julian days same as this PHP method (GregorianToJD). 回答1: According to http://en.wikipedia.org/wiki/Julian_day, the Julian day number for January 1, 2000, was 2,451,545. So you can compute the number of days between your date and this reference date. For example (Jan 1, 2014): NSUInteger julianDayFor01012000 = 2451545; NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [cal setTimeZone:[NSTimeZone

IOS how to set date format [duplicate]

删除回忆录丶 提交于 2019-11-29 19:17:18
问题 This question already has an answer here: Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset? 5 answers I have a string like this NSString *datestr = @"2013-08-06T03:51:54+00:00"; I try to set dateformat as below: NSDateFormatter *dformat = [[NSDateFormatter alloc]init]; [dformat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"]; NSDate *date = [dformat dateFromString:datestr]; But the date will be nil, so, can anyone tell me how to set the dateformat, thx! 回答1:

Get current NSDate in timestamp format

亡梦爱人 提交于 2019-11-29 18:50:26
I have a basic method which gets the current time and sets it in a string. However, how can I get it to format the current date & time in a UNIX since-1970 timestamp format? Here is my code: NSDate *currentTime = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"hh-mm"]; NSString *resultString = [dateFormatter stringFromDate: currentTime]; Is it possible to use NSDateFormatter to change the 'resultString' into a timestamp? Here's what I use: NSString * timestamp = [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970]

Error: Binary operator '<=' cannot be applied to operands of type 'Int?' and 'Int'

笑着哭i 提交于 2019-11-29 18:26:29
I am getting a error named: Binary operator '<=' cannot be applied to operands of type 'Int?' and 'Int' on line: if differenceOfDate.second <= 0 Can someone please help! let fromDate = Date(timeIntervalSince1970: TimeInterval(truncating: posts[indexPath.row].postDate)) let toDate = Date() var calendar = Calendar.current let components = Set<Calendar.Component>([.second, .minute, .hour, .day, .weekOfMonth, .month]) let differenceOfDate = Calendar.current.dateComponents(components, from: fromDate, to: toDate) if differenceOfDate.second <= 0 { cell.DateLbl.text = "now" }else if differenceOfDate