nsdate

NSDateFormatter and yyyy-MM-dd

六眼飞鱼酱① 提交于 2019-11-26 14:45:29
问题 I'm trying to take a NSString date in the format "2/22/11" and convert it to this format: 2011-02-22 This is my code: NSDate *dateTemp = [[NSDate alloc] init]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy-MM-dd"]; dateTemp = [dateFormat dateFromString:newInvoice.date]; newInvoice.date = [dateFormat stringFromDate:dateTemp]; newInvoice.date starts as an NSString equal to "2/22/11". dateTemp ends up NIL. newInvoice.date ends up NIL as well. I

All dates between two Date objects (Swift)

∥☆過路亽.° 提交于 2019-11-26 14:39:30
I’m creating a date using NSDateComponents() . let startDate = NSDateComponents() startDate.year = 2015 startDate.month = 9 startDate.day = 1 let calendar = NSCalendar.currentCalendar() let startDateNSDate = calendar.dateFromComponents(startDate)! ... now I want to print all dates since the startDate until today, NSDate() . I’ve already tried playing with NSCalendarUnit , but it only outputs the whole difference, not the single dates between. let unit: NSCalendarUnit = [.Year, .Month, .Day, .Hour, .Minute, .Second] let diff = NSCalendar.currentCalendar().components(unit, fromDate:

NSDateFormatter return wrong date + Swift

时间秒杀一切 提交于 2019-11-26 14:29:21
问题 Code : let dateString = "2016-04-02" var formatter: NSDateFormatter = NSDateFormatter() formatter.timeZone = NSTimeZone(abbreviation: "GMT +3:00") formatter.dateFormat = "yyyy-MM-dd" println("dateString: \(dateString)") formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") let date = formatter.dateFromString(dateString) println("date: \(date)") formatter.dateFormat = "yyyy-MM-dd" let formattedDateString = formatter.stringFromDate(date!) println("formattedDateString: \

NSTimeInterval to NSDate

空扰寡人 提交于 2019-11-26 14:24:12
问题 How can I convert a NSTimeInterval to NSDate ? Think of it like a stopwatch. I want the initial date to be 00:00:00, and I have a NSTimeInterval of X seconds. I need to do it like this because the NSTimeInterval needs to be converted to an int by using lround to round up, then converted to a NSDate to use the NSDateFormatter to throw it into a string. 回答1: An NSTimeInterval , as its name, um, implies, doesn't represent the same thing as an NSDate . An NSDate is a moment in time. A time

Date Format in Swift

依然范特西╮ 提交于 2019-11-26 14:06:45
How will I convert this datetime from the date? From this: 2016-02-29 12:24:26 to: Feb 29, 2016 So far, this is my code and it returns a nil value: let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "MM-dd-yyyy" dateFormatter.timeZone = NSTimeZone(name: "UTC") let date: NSDate? = dateFormatter.dateFromString("2016-02-29 12:24:26") print(date) You have to declare 2 different NSDateFormatters , the first to convert the string to a NSDate and the second to print the date in your format. Try this code: let dateFormatterGet = NSDateFormatter() dateFormatterGet.dateFormat = "yyyy-MM-dd

Removing time components from an NSDate object using Objective C/Cocoa

六月ゝ 毕业季﹏ 提交于 2019-11-26 14:06:27
问题 I'm trying to write a simple function using Objective C that accepts an NSDate object and returns an NSDate object that contains the same date value but has removed any time components from the date. For example if I were to pass an NSDate with a value of '2010-10-12 09:29:34' the function would return 2010-10-12 00:00:00. The I'm using function seems to work properly. However, when I test the iPhone app I'm developing with Instruments it reports there is a memory leak in the function I'm

Why NSDateFormatter can not parse date from ISO 8601 format [duplicate]

岁酱吖の 提交于 2019-11-26 13:45:42
问题 Possible Duplicate: Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset? I use rails as backend, the default date output is 2008-12-29T00:27:42-08:00 But after my research NSDateFormatter can not support it, except I change date out to 2008-12-29T00:27:42-0800 Here is the code I used to parse ISO 8601 date, but it's not work NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm

Get NSDate from NSDate adjusted with timezone

五迷三道 提交于 2019-11-26 13:22:21
I have a NSDate object. Let's say it represents "1-10-2011" NSDate *date = [df dateFromString:@"2011-10-01 00:00:00"]; That date translates into "2011-09-30 22:00:00" because of my timezone. Question: How do I get a new Date object representing "2011-10-01 00:00:00" in my local timezone? NSDate only represents an absolute point in time . It has no concept of timezone or calendar. When you create a NSDate instance it is just a number of seconds since January 1st 2001 GMT! It does not matter if you are in New York, Tokyo, Barcelona or Jerusalem. At your example, you instance the NSDate based on

From String to NSDate in Swift

旧时模样 提交于 2019-11-26 12:47:46
问题 I\'ve got a string containing a date in this format: Thu, 04 Sep 2014 10:50:12 +0000 Do you know how can I convert it to: 04-09-2014 ( dd-MM-yyyy ) of course, using Swift 回答1: If the above doesn't work, this should do the trick: let formatter = DateFormatter() formatter.locale = Locale(identifier: "US_en") formatter.dateFormat = "E, dd MMM yyyy HH:mm:ss Z" let date = formatter.date(from: "Thu, 04 Sep 2014 10:50:12 +0000") 回答2: I know the answer to this question has already been accepted but

Sort NSArray of custom objects by their NSDate properties

怎甘沉沦 提交于 2019-11-26 12:43:39
问题 I am attempting to sort an NSArray that is populated with custom objects. Each object has a property startDateTime that is of type NSDate. The following code results in an array, sortedEventArray , populated but not sorted. Am I going about this the completely wrong way or am I just missing something small? NSSortDescriptor *dateDescriptor = [NSSortDescriptor sortDescriptorWithKey:@\"startDateTime\" ascending:YES]; NSArray *sortDescriptors = [NSArray arrayWithObject:dateDescriptor]; NSArray