nsdate

NSDate expressed in different time zones, i.e. local time zone (GMT-400) to PST

耗尽温柔 提交于 2019-12-18 04:24:13
问题 I know how to use NSTimeZone to derive the offset for the current time in another time zone. NSDate always returns relative to GMT, so how do I derive a string with the proper time zone information? i.e. I take the current time where I am (in EST) and using NSTimeZone end up subtracting the 3 hours necessary to represent the time in PST. But all I've done is subtract 3 hours from the time which is still represented relative to my time zone. How do I get NSDateFormatter to spit out the time

How to get the hour of the day with Swift?

China☆狼群 提交于 2019-12-17 22:32:00
问题 How would I get the hour of the day in Swift. I have tried NSCalendar and NSDateComponents , but I'm afraid I'm just starting with Swift. 回答1: Swift 5.0 / 4.0 / 3.0 let hour = Calendar.current.component(.hour, from: Date()) Or, if you're interested in 12 hour AM/PM date format, then use NSDateFormatter let formatter = DateFormatter() formatter.dateFormat = "hh a" // "a" prints "pm" or "am" let hourString = formatter.string(from: Date()) // "12 AM" If you want minutes, seconds and others, do

GMT time on iPhone

可紊 提交于 2019-12-17 22:25:07
问题 How do I get GMT time? NSDate *c =[NSDate date]; gives system time, not GMT. 回答1: - (NSDate*) convertToUTC:(NSDate*)sourceDate { NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone]; NSTimeZone* utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"]; NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:sourceDate]; NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:sourceDate]; NSTimeInterval gmtInterval = gmtOffset - currentGMTOffset; NSDate* destinationDate = [[

Bad converting String to NSDate

僤鯓⒐⒋嵵緔 提交于 2019-12-17 21:23:41
问题 I have a problem with converting String to NSDate. In my text field when user tap on it, is a possibility to chose a date using UIDatePicker . String as a date in TextField looks like that: "yyyy-mm-dd" Now when I try to convert date for example on 2016-05-13 using this method: func changeStringToNSDate(){ let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-mm-dd" let from = moreSorts.fromDatePickerTextField.text! let fromDate = dateFormatter.dateFromString(from) print

Format a date from a string

跟風遠走 提交于 2019-12-17 19:31:15
问题 I'm trying to format a date from a string into another format. For example: 2012-05-29 23:55:52 into 29/05 *newline* 2010 . I just don't get the logics behind NSDate and NSDateFormatter, I think.. Any help will be appreciated. Thanks :) 回答1: You will need to create an NSDateFormatter, and then set it's dateFormat to match the first date you have, eg: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; That will set your date

How to find what day of the week for any given date using Cocoa

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 19:19:15
问题 I'm trying to figure out what day (i.e. Monday, Friday...) of any given date (i.e. Jun 27th, 2009) Thank you. 回答1: I've been doing: NSDateFormatter* theDateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; [theDateFormatter setDateFormat:@"EEEE"]; NSString *weekDay = [theDateFormatter stringFromDate:[NSDate date]]; This has the added bonus of letting you choose how you'd like the weekday to be returned (by changing

Determine if today's date is in a range of two dates on iOS [duplicate]

时光毁灭记忆、已成空白 提交于 2019-12-17 18:42:10
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: How to compare two dates in Objective-C I'd like to pop up a message in one of my apps when the date is in the range of two dates (e.g. the holiday period) So like if(dateInRangeof:date1, date2){True}else{false} Looking for any code snippets or apis to look at. Cheers 回答1: - (BOOL)isDate:(NSDate *)date inRangeFirstDate:(NSDate *)firstDate lastDate:(NSDate *)lastDate { return [date compare:firstDate] ==

Compare two NSDates for same date/time [duplicate]

十年热恋 提交于 2019-12-17 18:37:47
问题 This question already has answers here : How to compare two dates in Objective-C (14 answers) Closed 6 years ago . Is date1 == date2 not a valid way to compare? If not, what is the correct alternative? Here's my code: - (NSDate*) dateWithNoTime { unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSCalendar* calendar = [NSCalendar currentCalendar]; NSDateComponents* components = [calendar components:flags fromDate:self]; NSDate* dateOnly = [calendar

Getting the last day of a month

断了今生、忘了曾经 提交于 2019-12-17 17:36:49
问题 How can I get the last day of the current month as an NSDate ? 回答1: NSDate *curDate = [NSDate date]; NSCalendar* calendar = [NSCalendar currentCalendar]; NSDateComponents* comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:curDate]; // Get necessary date components // set last of month [comps setMonth:[comps month]+1]; [comps setDay:0]; NSDate *tDateMonth = [calendar dateFromComponents:comps]; NSLog(@"%@", tDateMonth); should

How to find weekday from today's date using NSDate?

假装没事ソ 提交于 2019-12-17 15:49:07
问题 I know that I can figure out today's date by [NSDate date]; but how would I find out today day of the week, like Saturday, Friday etc. I know that %w can be used in NSDateFormatter , but I don't know to use it. 回答1: NSCalendar* cal = [NSCalendar currentCalendar]; NSDateComponents* comp = [cal components:NSCalendarUnitWeekday fromDate:[NSDate date]]; return [comp weekday]; // 1 = Sunday, 2 = Monday, etc. See @HuguesBR's answer if you just need the weekday without other components ( requires