nsdate

Convert string to date in Swift

本小妞迷上赌 提交于 2019-11-27 07:02:15
How can I convert this string "2016-04-14T10:44:00+0000" into an NSDate and keep only the year, month, day, hour? The T in the middle of it really throws off what I am used to when working with dates. Convert the ISO8601 sting to date let isoDate = "2016-04-14T10:44:00+0000" let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX let date = dateFormatter.date(from:isoDate)! Get the date components for year, month, day and hour from the date let calendar = Calendar.current

NSDate of yesterday

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 06:57:48
How do I create an NSDate object with a custom date other than the current date? For example I would like to create a var of yesterday or of 2 days ago. You should use NSCalendar for calculating dates. For example, in Swift 3 the date two days before today is: let calendar = Calendar.current let twoDaysAgo = calendar.date(byAdding: .day, value: -2, to: Date()) Or in Swift 2: let calendar = NSCalendar.currentCalendar() let twoDaysAgo = calendar.dateByAddingUnit(.Day, value: -2, toDate: NSDate(), options: []) Or to get the first of the month, you can get the day, month and year from the current

get current date from [NSDate date] but set the time to 10:00 am

匆匆过客 提交于 2019-11-27 06:51:36
How can I reset the current date retrieved from [NSDate date] but then change the time to 10:00 in the morning. Matthias Bauch As with all date manipulation you have to use NSDateComponents and NSCalendar NSDate *now = [NSDate date]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian]; NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now]; [components setHour:10]; NSDate *today10am = [calendar dateFromComponents:components]; in iOS8 Apple introduced a convenience method that

How to get nsdate with millisecond accuracy?

对着背影说爱祢 提交于 2019-11-27 06:36:06
问题 I need to get time in millisecond accuracy. How can I get it from NSDate . Currently when I NSLog time, it is showing only upto seconds. 回答1: You'll need to use the below method to convert sec. into millisecond: ([NSDate timeIntervalSinceReferenceDate] * 1000) 回答2: For those who want to get the time in mili seconds (like in Java) double timestamp = [[NSDate date] timeIntervalSince1970]; int64_t timeInMilisInt64 = (int64_t)(timestamp*1000); (tested with iOS7 and the iPhone simulator using

Get the date of next saturday in Objective-C?

爱⌒轻易说出口 提交于 2019-11-27 06:28:16
问题 How would you get the date of the the first saturday that occurs after today in Objective-C for iOS? 回答1: This will guide you through the process of manipulating calendar dates: Calendrical Calculations 回答2: NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *date = [NSDate date]; NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date]; NSInteger todayWeekday = [weekdayComponents weekday]; enum Weeks {

Get current iPhone device timezone date and time from UTC-5 timezone date and time iPhone app?

点点圈 提交于 2019-11-27 06:19:30
In my iPhone application i am very struggling to convert the webservice response time to current device time. The webservice time in UTC-5 timezone . The app is worldwide use app. So i have to convert the response time to current device timezone. This is simple messaging app. When i have sent a message the time is "5:05 PM" (India Chennai timezone) but when am retrieve time from webservice is "2012-07-16 07:33:01" . When i show the time in the app i should convert the time to device's local time. I have tried in some way but i can't solve yet. Code is: NSDateFormatter *dateFormatter1 = [

Difference between two NSDate objects — Result also a NSDate

扶醉桌前 提交于 2019-11-27 06:19:08
I have two NSDate objects and I want the difference between the two and the result should again be a NSDate object. Any idea how to achieve this? Here, I am trying to address a unique problem where I have to find out the elapsed time and then localize the elapsed time. I can localize it if I have the elapsed time in NSDate object. So thought of creating a NSDate object which has its time component same as time interval between the two dates so that I could use NSDateFormatter to localize it. NSDate represents an instance in time, so it doesn't make sense to represent an interval of time as an

Converting string MM/DD/YYYY to NSDate

ⅰ亾dé卋堺 提交于 2019-11-27 06:01:11
问题 In my app i'm using Facebook Graph API and when I'm fetching user details i get the user's birthday as a string with format MM/DD/YYYY. My question is, how can I convert string "MM/DD/YYYY" to NSDate ? I try doing NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"MM/DD/YYYY"]; NSDate *date = [dateFormatter dateFromString:@"08/08/2012"]; [dateFormatter release]; But I got the wrong date 回答1: Using NSDateFormatter you can convert directly from a

Objective C - How can i get the weekday from NSDate?

人走茶凉 提交于 2019-11-27 05:55:06
问题 I need to be able to get the weekday from nsdate, i have the following code and it always return 1. I tried to change the month, i tried everything from 1 to 12 but the result of the week day is always 1. NSDate *date2 = [[NSDate alloc] initWithString:[NSString stringWithFormat:@"%d-%d-%d", 2010, 6, 1]]; unsigned units2 = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit; NSCalendar *calendar2 = [[NSCalendar alloc] initWithCalendarIdentifier

Converting a date format in objective C

陌路散爱 提交于 2019-11-27 05:49:03
问题 How can i convert the following date "Sun Jul 17 07:48:34 +0000 2011" to the following format "2011-07-17 07:48:34"? I used NSDateFormatter as shown below but it didnt work. It gives null as a result. NSDateFormatter *objDateFormatter = [[NSDateFormatter alloc] init]; [objDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; [objDateFormatter dateFromString:sDate]; 回答1: You've got your Date Format wrong for the style of Date you are passing in. Here is a document explaining the different