nsdate

Convert string with unknown format (any format) to date

杀马特。学长 韩版系。学妹 提交于 2019-11-26 15:29:53
I have data that contains a date string. Normally it would be in a 'Jan. 3, 1966' type format, but because of international differences, it may not always be exactly that. I need to read the data in and convert it into a standard date string ('YYYY-MM-DD'). Basically this is what I have so far: var dataString = 'Jan. 3, 1966' var dateFormatter = NSDateFormatter() dateFormatter.dateFormat = # I DON'T KNOW THE EXACT INPUT FORMAT ! let dateValue = dateFormatter.dateFromString(dataString) Xcode 9 • Swift 4 or Xcode 8.3.2 • Swift 3.1 You can use NSDataDetector as follow: extension String { var

returns a date an hour in the future

做~自己de王妃 提交于 2019-11-26 15:28:17
I'm getting the current date/time using [NSDate date] . The value returned is an hour in the future. I've check my phones location & time settings and they are correct. I can display the correct date and time as a string using the code below. [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle] But I need it to return the correct date/time as a date object as I use it to calculate the estimated time of arrival using - [date dateByAddingTimeInterval:interval] I realise my question is similar to this one already asked but

Formatting date and time with iPhone SDK?

别说谁变了你拦得住时间么 提交于 2019-11-26 15:26:36
问题 How do I format date and time using iPhone SDK? For example, how would I extract the date from 9/4/2024 12:00:00 AM to NSDate? 回答1: you can format your string using dd/MM/yyyy hh:mm:ss a example as below: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd/MM/yyyy hh:mm:ss a"]; NSLog(@"%@", [dateFormatter dateFromString:@"12/12/2012 12:12:12 AM"]); 回答2: some more option Now you want all the string formats that can be used with NSDateFormatter.

How do I get hour and minutes from NSDate?

谁说我不能喝 提交于 2019-11-26 15:08:18
问题 In my application I need to get the hour and minute separately: NSString *currentHour=[string1 substringWithRange: NSMakeRange(0,2)]; int currentHourInNumber=[currentHour intValue]; Consider string1 contains 11:59:13 AM which is coming from datepicker. Here if I use above code, it's okay to get hour if it's greater than 9. Else I need to change NSMakeRange(0,1) to get hour between 1 to 9. Are there any methods to get the hour, minutes, etc? Thanks in advance, please provide me sample code.

Comparing two NSDates and ignoring the time component

不羁岁月 提交于 2019-11-26 15:04:24
What is the most efficient/recommended way of comparing two NSDates? I would like to be able to see if both dates are on the same day, irrespective of the time and have started writing some code that uses the timeIntervalSinceDate: method within the NSDate class and gets the integer of this value divided by the number of seconds in a day. This seems long winded and I feel like I am missing something obvious. The code I am trying to fix is: if (!([key compare:todaysDate] == NSOrderedDescending)) { todaysDateSection = [eventSectionsArray count] - 1; } where key and todaysDate are NSDate objects

NSdate Assuming wrong time zone

限于喜欢 提交于 2019-11-26 14:58:36
问题 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..

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

天大地大妈咪最大 提交于 2019-11-26 14:56:23
问题 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..!! 回答1: 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 *

swift NSDateFormatter not working

空扰寡人 提交于 2019-11-26 14:47:52
I'm trying to format time as follows: let formatter = NSDateFormatter() formatter.dateFormat = "hh_mm_ss" let d = formatter.stringFromDate(NSDate()) println("formatted text is: \(d)") Suppose that current time is Sep 3, 2014 22:15:30 When I run this script on playground, it prints the time correctly formatted: 22_15_30 . When running this on AppDelegate, it doesn't print the time formatted: 22:15:30 I'm using xcode 6 beta 5... Am I missing something? Why stringFromDate doesn't return the correct date formatted? EDIT : I'm using xcode 6 beta 6. Thanks! Martin R The 24-hour format is "HH", not

How to Get time difference in iPhone

余生长醉 提交于 2019-11-26 14:46:39
问题 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 ? 回答1: Your problem has two parts, parsing the time and getting the difference: NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init]

Convert NSString of a date to an NSDate

折月煮酒 提交于 2019-11-26 14:45:47
问题 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? 回答1: NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"MM/dd/yyyy h:mm a"]; NSDate *date