nsdate

Compare current time with two times-of-day strings [closed]

可紊 提交于 2019-11-27 09:54:00
How can i get time like "11:30" formate so that i want to compare it with the following: strOpenTime = @"10:00"; strCloseTime = @"2:00"; so how can i get current time like as above open/close time format and i want if the current time is inside the interval open/close time? Thanks in advance..!! First you have to convert the strings "10:00", "2:00" to a date from the current day . This can be done e.g. with the following method (error checking omitted for brevity): - (NSDate *)todaysDateFromString:(NSString *)time { // Split hour/minute into separate strings: NSArray *array = [time

NSdate Assuming wrong time zone

一个人想着一个人 提交于 2019-11-27 09:52:32
I am trying to convert the following string into an NSDate object: NSString *str=@"25 May 2012 10:25:00"; NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateFormat:@"d MMM yyyy HH:mm:ss"]; [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"asia/kolkata"]]; NSDate *date = [dateFormatter dateFromString:str]; In console : date-->2012-05-25 04:55:00 +0000....it lags behind 5 hours and 30 minutes and assumes GMT timezone instead of Asia...Why it is so? When you see an [NSDate description] printed in the console, it is always the corresponding

How to Get time difference in iPhone

痞子三分冷 提交于 2019-11-27 09:30:42
I have 2 arrays with time values in it. They are in the following format. mm:ss:hundreds of a sec. I want to get the difference between the two [lastObjects] in the arrays. NSDate is not working because the last value is in hundredsth of a sec. A question. If the second date is larger than the first will it give me a negative number like -01:10:00 ? Your problem has two parts, parsing the time and getting the difference: NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease

Convert NSString of a date to an NSDate

拟墨画扇 提交于 2019-11-27 09:27:34
This might be a silly question, but I can't seem to find the answer on here or in the documentation. I want to convert an NSString such as @"9/22/2010 3:45 PM" to an NSDate. I know to use NSDateFormatter, but the problems are The month could be one or two digits Likewise, the date could be one or two digits Hours could be one or two digits What do I do about AM/PM? NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"MM/dd/yyyy h:mm a"]; NSDate *date = [dateFormat dateFromString:dateStr]; [dateFormat release]; there is no problem in 2 digit day or 2 digit

NSDateFormatter and yyyy-MM-dd

寵の児 提交于 2019-11-27 09:27:05
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 can't for the life of me figure out why. You are facing this problem because your date formatter is not

iPhone OS: How do I create an NSDate for a specific date?

旧城冷巷雨未停 提交于 2019-11-27 09:23:05
问题 Seems like a simple thing but I can't seem to find a way to do it. It would be great to see a couple different methods. 回答1: @Chuck's answer is correct, and lead me to the following code. Thought I'd share: NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setDay:10]; [comps setMonth:10]; [comps setYear:2010]; NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:comps]; 回答2: You can use this NSString *dateString = @"03-Sep-11"; NSDateFormatter *dateFormatter = [

From String to NSDate in Swift

ぃ、小莉子 提交于 2019-11-27 09:21:26
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 Thanks csch 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") I know the answer to this question has already been accepted but the shown method in that answer doesn't exactly output the date in the format of dd-MM-yyyy . So I'm

NSDateFormatter return incorrect date from string

微笑、不失礼 提交于 2019-11-27 09:18:54
I have a method, + (NSDate *) convertToDateFrom:(NSString *) dateString { if (dateString == nil || [dateString isEqual:@""]) return nil; //return nil if dateString is empty NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"EEEE, dd MMMM yyyy HH:mm"]; NSDate *date = [df dateFromString:dateString]; return date; } When I pass, @"Monday, 21 November 2011 17:01" //Passed string It returns a wrong date, 2011-11-21 23:14:00 +0000 // Output I am not sure whether I am using those flags correctly or NSDateFormatter isn't properly converting my string to date. Am I doing something

NSTimeInterval to NSDate

大憨熊 提交于 2019-11-27 08:53:51
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. 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 interval is a stretch of time. To get a point from an interval, you have to have another point. Your question is

Convert NSString->NSDate?

有些话、适合烂在心里 提交于 2019-11-27 08:49:42
问题 I need to convert my NSString in the end to an NSDate but I am having problems doing so. How would I achieve the same functionality with this code while making the end result an NSDate? //Date and Format it NSDate *now = [NSDate date]; NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease]; [formatter setDateFormat:@"MM-dd-yyyy"]; NSString *dateString = [formatter stringFromDate:now]; Thanks! Edit: //Date and Format it NSDate *now = [NSDate date]; NSDateFormatter* formatter