nsdate

Swift 3: Date vs NSDate?

早过忘川 提交于 2020-01-27 03:50:58
问题 What is the difference between the NS and non NS classes? Particularly NSDate vs Date ? Does NS represent some type of wrapper around the core non NS functionality? 回答1: Swift 3 introduced some new overlay value types for existing Foundation class types, such as Date for NSDate , Data for NSData and some more. The full list and details can be found in SE-0069 Mutability and Foundation Value Types Some of the reasons were Provide proper value semantics, let and var instead of mutable and

How to convert NSString to NSDate using NSDateFormatter?

限于喜欢 提交于 2020-01-24 21:35:07
问题 I getting the date from the webservice like "2012-07-03 07:26:48". I have saved in NSString after parsing from webservice. While convert the NSString to NSDate used below code, NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; [dateFormatter1 setDateFormat:@"yyyy-MM-dd hh:mm:ss"]; NSString *dateStr = @"2012-07-03 07:26:48"; NSDate *date = [dateFormatter1 dateFromString:dateStr]; NSLog(@"Date : %@", date); The NSLog value is "2012-07-03 01:56:48 +0000"; The NSLog value is "2012

How to get the date a week in the past from today?

点点圈 提交于 2020-01-24 12:52:38
问题 I know how to get the date in iOS. I need to find last weeks date. So for example if today is 05/27/2011, I need to be able to get 05/20/2011 Thanks 回答1: How about: NSCalendar * calendar = [NSCalendar currentCalendar]; NSDate * date = [NSDate date]; NSDateComponents *components = [[NSDateComponents alloc] init]; [components setWeek:-1]; NSDate * lastweek = [calendar dateByAddingComponents:components toDate:date options:0]; NSLog(@"%@", lastweek); [components release]; 回答2: This is crude but

How to get the date a week in the past from today?

两盒软妹~` 提交于 2020-01-24 12:51:28
问题 I know how to get the date in iOS. I need to find last weeks date. So for example if today is 05/27/2011, I need to be able to get 05/20/2011 Thanks 回答1: How about: NSCalendar * calendar = [NSCalendar currentCalendar]; NSDate * date = [NSDate date]; NSDateComponents *components = [[NSDateComponents alloc] init]; [components setWeek:-1]; NSDate * lastweek = [calendar dateByAddingComponents:components toDate:date options:0]; NSLog(@"%@", lastweek); [components release]; 回答2: This is crude but

Geting day number from NSDate

孤者浪人 提交于 2020-01-24 01:07:29
问题 I need to get the day of the week from an NSDate as a number. For example, if today is Thursday I should get number 4, if Friday then number 5. Does this functionality already exist? 回答1: As per your requirement the following codes give you '4' for today i.e. Thursday 13 Dec 2012 NSMutableDictionary *weeks=[NSMutableDictionary new]; [weeks setObject:@"1" forKey:@"Monday"]; [weeks setObject:@"2" forKey:@"Tuesday"]; [weeks setObject:@"3" forKey:@"Wednesday"]; [weeks setObject:@"4" forKey:@

How to get start date and end date of the current month (Swift 3)

二次信任 提交于 2020-01-23 07:00:29
问题 I'm trying to get the start and end dates of the current month in dd/MM/yyyy format. I tried using extension as answered in this SO Question.But it seems like it's not what I want(the format is different and also it's giving me last month's last date and current month last but one date ). Can some one help me. Extension Class: extension Date { func startOfMonth() -> Date? { let comp: DateComponents = Calendar.current.dateComponents([.year, .month, .hour], from: Calendar.current.startOfDay(for

Convert an NSDate to a Unix timestamp using a particular time zone

前提是你 提交于 2020-01-23 01:43:13
问题 I am trying to get the Unix timestamp using a particular time zone (not necessarily the same time zone as the system) I tried this : NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSTimeZone *gmt = [NSTimeZone timeZoneWithName:@"Australia/Melbourne"]; [dateFormatter setTimeZone:gmt]; NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]]; NSDate *curdate = [dateFormatter dateFromString:timeStamp]

dateFromString() returns incorrect date

有些话、适合烂在心里 提交于 2020-01-21 14:39:12
问题 I'm trying to convert string to Date, but it result incorrect date. let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "dd MMM YYYY" let dt = dateFormatter.dateFromString("17 Sep 2015") println("Date : \(dt)") It result Date : Optional(2014-12-20 18:30:00 +0000) Please let me know where I'm making mistake. I tried other format too, but it return nil. 回答1: The format for year is incorrect, it should be yyyy , not YYYY . "Y": Year (in "Week of Year" based calendars). This year

Swift NSDate showing UTC when printing in debugger. How do I know if it's local time or utc when comparing date/times?

拈花ヽ惹草 提交于 2020-01-17 15:18:05
问题 I'm trying to write some code for a scheduling app and these schedules need to be based on the user's current time. Example, something is available between 5pm and 9pm on the current day. When I get the current time by using NSDate(), printing it results in utc: 2016-12-15 13:12:05 +0000. I know that it's in utc because if I add my local time zone offset in my head, it works out to be the actual time on my phone. The problem I'm having is when trying to create these time windows, sometimes

Sort Array of Dictionaries by NSDates within the Dictionaries

孤者浪人 提交于 2020-01-17 04:23:45
问题 I am creating an application with separate posts in it (similar to Instagram). Each post is a dictionary located in one big array. Within each dictionary their is an NSDate which I would like to sort the dictionaries by in order from newest to oldest. I have read similar questions but they are in objective C. I have a feeling NSSortDescriptor be a helpful method but am unsure on how to use it in this situation. Any help would be great, Thanks 回答1: If you’re writing in Swift, you’ll probably