nsdate

How to get all the days/dates in current week, this month, this year using the current date (today) in iPhone

廉价感情. 提交于 2019-12-02 15:05:38
问题 In my application i have to show list of working hours by date in a table view, Here i have 5 scenarios Today's projects, working hours Selected date projects, hours Cureent week (sunday-saturday) Current month (working hours in january, February if it is feb march if it is march) Current year (all working hours in 2013) i have implemented sqlite queries as follows, 1. Select Date, ProjectTitle, WorkingHours, sum(WorkingHours) from ProjDetailsTable where date = toDaysDate 2. Select Date,

Get Current date & time with [NSDate date]

烈酒焚心 提交于 2019-12-02 14:21:35
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 Parag Bafna 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-MM-dd hh:mm:ss a" if you prefer the time with AM/PM NSLog(@"%@",[dateFormatter stringFromDate:

NSDate from stange looking string

南笙酒味 提交于 2019-12-02 14:18:28
问题 I have an NSDictionary with the value for date as: "Date":"/Date(1314313200000+0100)/", How can I turn this into an NSDate as it contains strings lie "Date": and / ? 回答1: 1314313200000 is milliseconds since the epoch-date (1970-01-01), and 0100 is the timezone. You need to parse this info out from the string and build a date from it. The NSScanner class is a good fit for parsing information out of strangely formatted text. // Init a scanner with your date string NSScanner* scanner =

Subtract 7 days from current date

妖精的绣舞 提交于 2019-12-02 14:13:24
It seems that I can't subtract 7 days from the current date. This is how i am doing it: NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *offsetComponents = [[NSDateComponents alloc] init]; [offsetComponents setDay:-7]; NSDate *sevenDaysAgo = [gregorian dateByAddingComponents:offsetComponents toDate:[NSDate date] options:0]; SevenDaysAgo gets the same value as the current date. Please help. EDIT: In my code I forgot to replace the variable which gets the current date with the right one. So above code is functional. use

Converting a NSString into NSDate

浪尽此生 提交于 2019-12-02 14:10:08
问题 I have date string: Monday, October 11, 2010. How can I create a NSDate object out of it and then get different components like day, month, date, year from it. Please note that format/locale of this string may change at runtime. 回答1: another possibility: NSCalendar *calendar = [NSCalendar currentCalendar]; NSInteger units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSDateComponents *components = [calendar components:units fromDate:date]; NSInteger year = [components year];

Get current date in Swift 3? [closed]

蹲街弑〆低调 提交于 2019-12-02 14:05:40
How can I set label.text current date in Swift 3? I want to print just today to the screen. I did not find how to do that. In c# is very simple: var date = DateTime.Now I need to write 15.09.2016 in swift 3. thanks You say in a comment you want to get "15.09.2016". For this, use Date and DateFormatter : let date = Date() let formatter = DateFormatter() Give the format you want to the formatter: formatter.dateFormat = "dd.MM.yyyy" Get the result string: let result = formatter.string(from: date) Set your label: label.text = result Result: 15.09.2016 Jorge Casariego You can do it in this way with

NSDate Output incorrectly [duplicate]

前提是你 提交于 2019-12-02 13:27:21
Possible Duplicate: Getting date from [NSDate date] off by a few hours NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"M-d-yyyy H:mm"]; NSDate *start= [dateFormatter dateFromString:@"10-24-2012 12:15"]; NSDate *end = [dateFormatter dateFromString:@"10-24-2012 15:30"]; When I print out NSLog(@"------main_event start %@", start); NSLog(@"-----main_event end %@", end); The result is ---main_event start 2012-10-24 19:15:00 +0000 ---main_event end 2012-10-24 22:30:00 +0000 Now, it looks like the time added 7 hours automatically, 12:15 becomes 19:15,

Getting current time from timezone, not the system/mobile time

十年热恋 提交于 2019-12-02 13:17:48
Can any one help me to get the current time of the time zone? Say for example if the user has an Indian timezone with mobile i.e. 5.00 PM, but he statically change his mobile time to 5.30 PM, here I need the actual timezone time, not the mobile time which the user changed as per his preference. If you need access to the world's clock, contact an NTP server. Go to Cocoapods and find a pod to do that. The example below is for ios-ntp : class ViewController: UIViewController { let netAssociation = NetAssociation(serverName: "time.apple.com") func viewDidLoad() { super.viewDidLoad() netAssociation

Swift NSDate iso 8601 format

夙愿已清 提交于 2019-12-02 12:59:45
问题 I am working on date formats in swift. Trying to covert string date to NSDate and NSSate to string date (ISO 8601 format) This is my code let stringDate = "2016-05-14T09:30:00.000Z" // iso 8601 format let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" //iso 8601 let date = dateFormatter.dateFromString(stringDate) print("Date = \(date)") // Output is 2016-05-14 16:30:00 +0000 // again converting it date to string using stringFromDate print("\

How to get the 'n' weekday of a Date

最后都变了- 提交于 2019-12-02 12:35:07
I need to translate this function into swift. Basically what does it get the 'n' day of the current week. So for example if i use it with NSDate().getWeekDay(0) it gives me Sun 11 Sept, and so on. But seems rangeOfUnit no longer exists in Swift-3. This was my previous implementation in Swift-2 extension NSDate { func getWeekDay(day: Int) -> NSDate { var beginningOfWeek: NSDate? NSCalendar.currentCalendar().rangeOfUnit(NSCalendarUnit.WeekOfYear, startDate: &beginningOfWeek, interval: nil, forDate: self) let comps = NSDateComponents() comps.day = day comps.minute = NSCalendar.currentCalendar()