nsdate

Comparing two dates with nsdate

天涯浪子 提交于 2019-12-02 20:04:40
问题 In my app I have to compare two times: current (which is 12:44) and given I tried: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"hh:mm"]; NSDate *neededDate = [[NSDate alloc] init]; neededDate = [dateFormatter dateFromString:@"14:45"]; if ([neededDate compare:[NSDate date]] == NSOrderedAscending) { NSLog(@"yes"); } else NSLog(@"no"); [neededDate release]; [dateFormatter release]; But any time I launch it, NSLog says "no". Please, suggest where

Get firstdate, lastdate of month?

不羁的心 提交于 2019-12-02 19:16:34
I want to get firstdate、lastdate of month,I try NSDateComponents *components = [calendar components:units fromDate:[NSDate date]]; [components setDay:1]; self.currentDate = [calendar dateFromComponents:components]; int m = components.month; int y = components.year; int d = components.day; log: 2012,5,1 How can i get lastdate od month? please give me some advice,thank you!! Some NSDate category methods I wrote will help you: Here's an easy way to find the start of a month: (NSDate *) startOfMonth { NSCalendar * calendar = [NSCalendar currentCalendar]; NSDateComponents * currentDateComponents =

Date format for 2016-10-20T13:01:47.317

微笑、不失礼 提交于 2019-12-02 18:50:01
问题 This is what I am trying for getting date from the string 2016-10-20T13:01:47.317 but getting nil NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; dateString = [dateString substringToIndex:dateString.length - 3]; [dateFormatter1 setDateFormat:@"YYYY-MM-ddTHH:mm:ss"]; NSDate *date = [dateFormatter1 dateFromString:dateString]; Also tried with yyyy but still getting nil. I was not able to understand .317 so I eliminated that from the string. What shall be the date format for

How to display time in 12 hour format in Objective-C?

僤鯓⒐⒋嵵緔 提交于 2019-12-02 18:34:32
I was working on an iPhone application, and I need to display the time in a label. So I wrote the follow: NSDate *date = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"HH:mm a"]; NSString *formattedDateString = [dateFormatter stringFromDate:date]; timeLabel.text = formattedDateString;** It displays time in label but in 24H format. Example: 15.00 PM instead of 3.00 PM I want to display time in 12h format, i.e. 3.00 PM What should I do? Thanks in advance. [dateFormatter setDateFormat:@"hh:mm a"]; Note the lower case h's. Date

Using core plot for iPhone, drawing date on x axis

丶灬走出姿态 提交于 2019-12-02 17:33:18
I have available an array of dictionary that contains NSDate and NSNumber values. I wanted to plot date on X axis. For plotting I need to supply xRanges to plot with some decimal values. I don't understand how can I supply NSDate values to xRange (low and length). And what should be there in this method: -(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index I mean how my date value will be returned as NSNumber ? I think I should use some interval over there, but what should be the exact conversion? Can any one explain me what are the exact

How to convert Unix timestamp into Swift NSDate object? [closed]

南楼画角 提交于 2019-12-02 17:31:11
I'm getting dates as Unix timestamps through my API and I want to convert them into NSDate objects in Swift. How can I do that? Try this: var date = NSDate(timeIntervalSince1970: timeInterval) 来源: https://stackoverflow.com/questions/26844132/how-to-convert-unix-timestamp-into-swift-nsdate-object

String description of NSDate

眉间皱痕 提交于 2019-12-02 17:14:27
I have the following code: [ [NSDate date] descriptionWithLocale: @"yyyy-MM-dd" ] I want it to return a date in the following format: "2009-04-23" But it returns: Thursday, April 23, 2009 11:27:03 PM GMT+03:00 What am I doing wrong? Thank you in advance. You are using the wrong method. Instead try descriptionWithCalendarFormat:timeZone:locale: [[NSDate date] descriptionWithCalendarFormat:@"%Y-%m-%d" timezone:nil locale:nil]; Also note that the method is expecting a different format than the one in your question. The full documentation for that can be found here . EDIT: Though as Mike noted,

Converting a gregorian date string to Islamic date gives correct & incorrect results

血红的双手。 提交于 2019-12-02 16:48:38
I have the following two date strings: (1) 24/04/2013 and (2) 19/03/2013 I'm trying to convert these dates into Islamic (Um Al Qura) dates, I'm using this code block to do so: NSDateFormatter *df = [[NSDateFormatter alloc] init]; df.dateFormat = @"dd/MM/yyyy"; df.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *dateInGrogrian = [df dateFromString:@"24/04/2013"]; NSDateFormatter *df2 = [[NSDateFormatter alloc] init]; NSCalendar * cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar]; [df2 setCalendar:cal]; [df2 setDateFormat:@"dd/MM/yyyy"

iPhone : How to find Dates between two selected dates?

狂风中的少年 提交于 2019-12-02 16:48:17
问题 In my app, I am getting two dates. For Example, 1st Date : 28-10-2011 2nd Date : 2-11-2011 Now I want to all dates between this two selected date. So it should be, 28-10-2011 29-10-2011 30-10-2011 1-11-2011 2-11-2011 How can I get this using NSDate? 回答1: try dateWithTimeInterval:SinceDate: NSMutableArray dates = [NSMutableArray array]; NSDate *curDate = startDate; while([curDate timeIntervalSince1970] <= [endDate timeIntervalSince1970]) //you can also use the earlier-method { [dates addObject

How can I figure out the day difference in the following example

别来无恙 提交于 2019-12-02 16:31:27
问题 I am calculating the day difference between two dates, but I figured out the the following code is actually giving me the difference for 24 hours rather than the difference in date. I have the following code: func daysBetweenDate(startDate: NSDate, endDate: NSDate) -> Int { let calendar = NSCalendar.currentCalendar() let components = calendar.components([.Day], fromDate:startDate, toDate: endDate, options: []) return components.day } So, for the following example I get this result: lastLaunch