nsdate

Create NSDate Monotouch

北战南征 提交于 2019-12-03 05:33:16
I am trying to take a date string and turn it into a specific NSDate (eg. July 1, 1981), but I don't see and methods for setting the date. Does anyone know how to accomplish this? Perhaps convert a DateTime object to NSDate? The easiest way is to set it from DateTime. REVISION: The NSDate conversion operators are now explicit, not implicit anymore! I updated the example below. If you look at the NSDate prototype you will find two operators: public static explicit operator NSDate(DateTime dt); public static explicit operator DateTime(NSDate d); These two will do the conversion for you. Explicit

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

核能气质少年 提交于 2019-12-03 05:17:36
问题 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

String description of NSDate

偶尔善良 提交于 2019-12-03 03:49:01
问题 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. 回答1: 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

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

最后都变了- 提交于 2019-12-03 03:17:43
问题 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]

iOS 8 - get current date as DD/MM/YYYY

纵饮孤独 提交于 2019-12-03 02:52:27
Can anybody give me some code how I get the date? NSDateComponents *components = [[NSCalendarcurrentCalendar] component:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYearfromDate:[NSDatedate]]; NSString *day = [components day]; NSString *week = [components month]; NSString *year = [components year]; NSString *date = [NSString stringWithFormat:@"%@.%@.%@",day,week,year]; ^^ my code is not working :S And is there a way that I can get the date of tomorrow, in 1 week and so on... Thanks :) Rob You can either use a variation of your code that retrieves the numeric components using

How to check whether now date is during 9:00-18:00

怎甘沉沦 提交于 2019-12-03 02:28:06
When my app is launched I want to check whether the date is between 9:00-18:00. And I can get the time of now using NSDate . How can I check the time? So many answers and so many flaws... You can use NSDateFormatter in order to get an user-friendly string from a date. But it is a very bad idea to use that string for date comparisons! Please ignore any answer to your question that involves using strings... If you want to get information about a date's year, month, day, hour, minute, etc., you should use NSCalendar and NSDateComponents . In order to check whether a date is between 9:00 and 18:00

How to properly convert the Last-Modified header from an HTTP response to an NSDate on iOS

蓝咒 提交于 2019-12-03 01:55:55
I am looking for a fully working solution, one that works with: any iOS locale or timezone any/most HTTP servers Xcode 4.0.2 (see why) Current broken code: NSString lastModifiedString = @"Mon, 06 Jun 2011 12:47:05 GMT"; NSDateFormatter *df = [[NSDateFormatter alloc] init]; //df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'"; df.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss z"; df.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]; df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; NSDate date = [df dateFromString:lastModifiedString]; I assumed that

NSDate day of the year (swift)

女生的网名这么多〃 提交于 2019-12-03 01:37:54
How might the day number of the year be found with swift? Is there a simple way that I'm not seeing, or do I have to find the number of seconds from Jan 1 to the current date and divide by the number of seconds in a day? Martin R This is a translation of the answer to How do you calculate the day of the year for a specific date in Objective-C? to Swift. Swift 2: let date = NSDate() // now let cal = NSCalendar.currentCalendar() let day = cal.ordinalityOfUnit(.Day, inUnit: .Year, forDate: date) print(day) Swift 3: let date = Date() // now let cal = Calendar.current let day = cal.ordinality(of:

Get Current date & time with [NSDate date]

点点圈 提交于 2019-12-03 01:00:27
问题 My system's date time is 26 May 22:55 but when i get date with [NSDate date] date time is 27 May 02:35 is it because of time zone ? if yes how to solve this problem, that when i get date time, give me the date of my system and doesn't check time zone 回答1: NSLocale* currentLocale = [NSLocale currentLocale]; [[NSDate date] descriptionWithLocale:currentLocale]; or use NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // or @"yyyy

Generate NSDate from day, month, and year [closed]

放肆的年华 提交于 2019-12-03 00:57:36
I'm trying to generate a NSDate from a month day and year (all in integer format). Right now my attempt is such: NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; NSNumber *day = [dataSource valueForKey:@"day"]; NSNumber *month = [dataSource valueForKey:@"month"]; NSNumber *year = [dataSource valueForKey:@"year"]; [components setDay:[day intValue]]; [components setMonth:[month intValue]]; [components setMonth:[year intValue]]; NSDate *_date = [calendar dateFromComponents:components]; However, _date outputs the following when