nsdate

Convert string date format

别等时光非礼了梦想. 提交于 2019-11-27 17:18:46
问题 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? 回答1: 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

How do I get the current date in Cocoa

我怕爱的太早我们不能终老 提交于 2019-11-27 17:17:13
I'm getting started developing for the iPhone and as such I am looking at different tutorials online as well as trying some different things out myself. Currently, I'm trying to create a countdown until midnight. To get the number of hour, minutes, and seconds, I do the following (which I found somewhere): NSDate* now = [NSDate date]; int hour = 23 - [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay]; int min = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] minuteOfHour]; int sec = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] secondOfMinute]; countdownLabel.text = [NSString

How to calculate the age based on NSDate

流过昼夜 提交于 2019-11-27 17:12:44
how to calculate the age based on the birth date in this format 6/24/1976 mon/date/year... cobbal Many of these answers don't properly account for leap years and such, best is to use Apple's methods instead of dividing by constants. Swift let birthday: NSDate = ... let now = Date() let ageComponents = calendar.dateComponents([.year], from: birthday, to: now) let age = ageComponents.year Objective-C NSDate* birthday = ...; NSDate* now = [NSDate date]; NSDateComponents* ageComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitYear fromDate:birthday toDate:now options:0]; NSInteger

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

跟風遠走 提交于 2019-11-27 16:52:12
问题 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. 回答1: 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

How to store dates without times in Core Data

喜欢而已 提交于 2019-11-27 16:33:28
问题 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

RKValueTransformers “failed transformation of value to NSDate”

▼魔方 西西 提交于 2019-11-27 16:30:45
I'm using RestKit, and pulling in a RSS feed. I'm getting this error for each date related to RKValueTransformers saying (for example): "Failed transformation of value 'September 04, 2014 09:58:01 PDT' to NSDate: none of the 10 value transformers consulted were successful." It goes on to tell me: Failed transformation of value at keyPath 'pubDate.text' to representation of type 'NSDate' and: {NSLocalizedDescription=Input value is not a valid ISO 8601 string: 'September 01, 2014 14:53:30 PDT'} I believe that I'm pulling in the text correctly, but can't figure out if its that or if there's

Why NSDate is reporting the wrong date?

本秂侑毒 提交于 2019-11-27 16:16:10
Here is my code. NSDate *today = [NSDate date]; NSString *todayString = [[today description] substringToIndex:10]; NSLog(@"Today: %@", todayString); I am getting 2011-07-19 instead of 2011-07-18. Any ideas on what may be the problem? NSDate's description method returns times in UTC, which if you are in the US Eastern Time Zone during daylight savings time is 4 hours later than your wall clock time. In other words, at 10pm your time it is 2am the next day in UTC. The usual way to fix it is to use NSDateFormatter instead, and explicitly set the time zone if necessary. Ayush Goel NSDate *today =

NSDate, comparing two dates [duplicate]

梦想与她 提交于 2019-11-27 16:14:18
问题 This question already has an answer here: How to compare two dates in Objective-C 14 answers Ok, I have been working on this for a couple of days now and am stuck. What I am trying to do is compare the current time with a predetermined time. What I want to happen is, Everyday at certain times I want certain code to fire, but not before the time of day that I specify. so, basically if the "current time" is the same or equal to "predetermined time" then fire this code. Or fire this line of code

How to set seconds to zero for NSDate

陌路散爱 提交于 2019-11-27 15:59:38
问题 I'm trying to get NSDate from UIDatePicker , but it constantly returns me a date time with trailing 20 seconds. How can I manually set NSDate 's second to zero? 回答1: NSDate is immutable, so you cannot modify its time. But you can create a new date object that snaps to the nearest minute: NSTimeInterval time = floor([date timeIntervalSinceReferenceDate] / 60.0) * 60.0; NSDate *minute = [NSDate dateWithTimeIntervalSinceReferenceDate:time]; Edit to answer Uli's comment The reference date for

NSDate not returning correct date

大憨熊 提交于 2019-11-27 15:52:25
I am printing NSDate like this: NSDate *date = [NSDate date]; NSString *stringDate = [data description]; Right now, it's July 1, 2011 11:43 pm. My iPod even says that on the top bar. But stringDate prints out: 2011-07-02 03:43:46 +0000 This is obviously wrong. I have used NSDate millions of times but never had this problem. What could be wrong? Thanks Your region's time offset is -04:00 ? NSDate will automatically adjust the time offset when displaying the date. Try, NSString *str = [date descriptionWithLocale:[NSLocale currentLocale]]; It will show you the correct date. Are you in the GMT -4