nsdate

Convert string date format

懵懂的女人 提交于 2019-11-29 02:46:53
I have date in string in this formate "Tue, 23 Oct 2012 01:05:00 +0000" . Now i want to convert date format to " TUE OCT 23 2012 1:05AM ". What is the best way to do this? NSString *dateStr = @"Tue, 25 May 2010 12:53:58 +0000"; // Convert string to date object NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"EE, d LLLL yyyy HH:mm:ss Z"]; NSDate *date = [dateFormat dateFromString:dateStr]; But First Create a NSDateFormatter to convert the date string to a NSDate object. Create a second NSDateFormatter (or change the format string of the first) to convert

NSDateFormatter: how to convert date string with 'GMT' to local NSDate?

陌路散爱 提交于 2019-11-29 02:34:04
I need to convert a date string retrieved from a server, to local NSDate, the string looks like: "Fri, 30 Nov 2012 00:35:18 GMT" But now it is Friday, 30th Nov, 11:36 AM, so how do I convert the string to a meaningful local NSDate? I found: iPhone: NSDate convert GMT to local time But looks like it does the opposite. Note that NSDate's always store the date, internally, in GMT. You then use a date formatter to create a string in your local time zone. In this case, you are starting with a string so need to use the date formatter to create the date in the first place, and then use it again to

How to store dates without times in Core Data

旧时模样 提交于 2019-11-29 02:17:47
I've been trying to find a sensible way of storing daily data using Core Data on the iPhone. My app receives data in csv format, with a date but no time: date, cycles 2009-08-01, 123 2009-08-02, 234 2009-08-03, 345 2009-08-04, 456 When this data is stored, there should only be one record per day. I figured the best thing to do was create an NSDate to be stored, but strip out the time & time zone data. I can easily create an NSDate without hours, minutes or seconds using either NSDateComponents or an NSDateFormatter. However even when I set the time zone explicitly to UTC or to zero seconds

How can I format a date in Objective-C similar to the jquery.timeago library?

最后都变了- 提交于 2019-11-29 02:11:08
问题 I have a feed of items displayed in table cells, part of which is a date / timestamp in the past. In Objective-C, how can I accomplish formatting them in the same manner as the jquery.timeago plugin on the web? That is, taking in a date and outputting things like: 'just now' '2 minutes ago' '24 days ago' 'a month ago' I see there is an NSDate extension class here with methods such as dateWithDaysBeforeNow, dateWithMinutesBeforeNow, etc, but if there is a library out there that has already

Creating a Time Based Reminder App in iPhone

让人想犯罪 __ 提交于 2019-11-29 02:00:04
问题 I am working on time based reminder App. in which the user enter his reminder and time for the reminder. The problem is that how to continuously comparing the current time with the user defined time. Any sample code will greatly help. because i am stuck on this point. 回答1: Comparing the current time vs. the user defined one is not the right design pattern. UIKit offers the NSLocalNotification object that is a more high-level abstraction for your task. Below is a snip of code that create and

Cocoa - Localized string from NSDate, NSCalendarDate

我只是一个虾纸丫 提交于 2019-11-29 01:57:27
问题 I'm using NSDate to get a string such as "18 Jun 09", with code: NSDate *theDate = [NSDate date]; NSString *dateString = [theDate descriptionWithCalendarFormat:@"%d %b %y" timeZone:nil locale: nil]; This works, but only results in an English output. I need the output to be localized in the user's default language. Does Cocoa (Mac OS X 10.4, 10.5 in this case) provide a facility for this localization or do I have to manually localize for each case of day and & month names my self? (I have

create a NSDate from a string

一笑奈何 提交于 2019-11-29 01:46:43
问题 I have an NSString like this: @"3/15/2012 9:15 PM" and I would like to convert it to NSDate , I have done like this: NSString *str =@"3/15/2012 9:15 PM"; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"mm/dd/yyyy HH:mm"]; NSDate *date = [formatter dateFromString:]; NSLog(@"%@", date); // date = null Can you help me please, thanks. 回答1: Use the following solution NSString *str = @"3/15/2012 9:15 PM"; NSDateFormatter *formatter = [[NSDateFormatter alloc]

Sort array of objects by their NSDate property [duplicate]

佐手、 提交于 2019-11-29 01:44:43
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How to sort an NSMutableArray with custom objects in it? How does one sort an array of objects by accessing their NSDate properties? I know one can use [mutableArray sortUsingSelector:@selector(compare:)]; on an array of dates, but how does one do it by accessing a NSDate property on each element in the array, sorting the objects by earliest date to latest date? 回答1: Thanks for all the answers, I came across

Number of days between two NSDate objects

旧城冷巷雨未停 提交于 2019-11-29 00:10:17
I'm trying to compare two days of NSDate objects, I used at the first time NSDateComponents to calculate difference between my two dates Objects, but it is working correctly. NSString *dateStr =@"2012-11-05 15:00:00"; NSDate *date = [dateFormat dateFromString:dateStr]; NSDateComponents *datesDiff = [calendar components: NSDayCalendarUnit fromDate: date toDate: [NSDate date] options: 0]; That does not resolve my problem. Indeed when date1 = 2012-11-05 23:00:00 and date2 = 2012-11-06 10:00:00, the difference between the tow dates is 0 day and 11 hours. I am looking for something that allows me

Why does NSDateFormatter return nil date for these 4 time zones?

主宰稳场 提交于 2019-11-28 22:22:39
Try running this in iOS6 (haven't tested pre iOS6): NSDateFormatter *julianDayDateFormatter = nil; julianDayDateFormatter = [[NSDateFormatter alloc] init]; [julianDayDateFormatter setDateFormat:@"g"]; for (NSString *timeZone in [NSTimeZone knownTimeZoneNames]) { julianDayDateFormatter.timeZone = [NSTimeZone timeZoneWithName: timeZone]; NSDate *date = [julianDayDateFormatter dateFromString:[NSString stringWithFormat:@"%d", 2475213]]; if (date == nil) NSLog(@"timeZone = %@", timeZone); } and you get the following output: America/Bahia America/Campo_Grande America/Cuiaba America/Sao_Paulo Can