nsdate

NSDate date don't give me the right hour

谁说胖子不能爱 提交于 2019-11-28 13:01:55
问题 I'm trying to get the current date with : NSLog(@"DATE NOW : %@", [NSDate date]); But when I execute it, I have a difference of one hour between the date displayed and the current date. Log http://data.imagup.com/12/1171458807.5637png I found topics on questions like that but none of them gave me the answer. How can I change that ? 回答1: You need to set the proper timezone. The default, as you can see, is UTC. You can use a date formatter for doing that. NSDateFormatter * dateFormatter = [

Date string to NSDate swift

陌路散爱 提交于 2019-11-28 12:25:57
问题 i'm having an array fill with date string, which i would like to go through and check whether the date is today or yesterday. The date string could look like following: 2015-04-10 22:07:00 So far i've tried just to convert it using dateFormatter, but it keeps returning nil var dateString = arrayNews[0][0].date as NSString var dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss" var date = dateFormatter.dateFromString(dateString as String) println(date) the sudo

Wrong date comes back from NSDateComponents

倖福魔咒の 提交于 2019-11-28 12:17:01
问题 Why is this giving me the wrong date ? NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar]; NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]]; [components setYear:2013]; [components setMonth:06]; [components setDay:28]; [components setHour:5]; [components setMinute:00]; NSDate *startDate1 = [myCalendar dateFromComponents:components]; NSLog(@"start date is %@"

How to get nsdate with millisecond accuracy?

孤街浪徒 提交于 2019-11-28 11:55:20
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. Jhaliya You'll need to use the below method to convert sec. into millisecond: ([NSDate timeIntervalSinceReferenceDate] * 1000) 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 Xcode 5) Kees is right about the milliseconds calculation, but you might want to consider processing only the

Get the date of next saturday in Objective-C?

早过忘川 提交于 2019-11-28 11:47:29
How would you get the date of the the first saturday that occurs after today in Objective-C for iOS? This will guide you through the process of manipulating calendar dates: Calendrical Calculations NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *date = [NSDate date]; NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date]; NSInteger todayWeekday = [weekdayComponents weekday]; enum Weeks { SUNDAY = 1, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }; NSInteger moveDays=SATURDAY-todayWeekday; if

Converting string MM/DD/YYYY to NSDate

a 夏天 提交于 2019-11-28 11:13:38
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 Using NSDateFormatter you can convert directly from a NSString to a NSDate . Here's an example: NSDateFormatter* myFormatter = [[NSDateFormatter alloc] init];

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

房东的猫 提交于 2019-11-28 11:02:07
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:NSGregorianCalendar]; NSDateComponents *components2 = [calendar2 components:units2 fromDate:date2]; int startWeekDay =

How to format convert the string 2013-01-27T02:31:47+08:00 into NSDate

左心房为你撑大大i 提交于 2019-11-28 10:59:37
问题 I have tried many times to convert the string 2013-01-27T02:31:47+08:00 into NSDate. I have found the formatting guide by apple and copied its code and tried , but it doesn't work. NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init]; NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; [rfc3339DateFormatter setLocale:enUSPOSIXLocale]; [rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"]; [rfc3339DateFormatter

Format a date from a string

风流意气都作罢 提交于 2019-11-28 10:27:01
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 :) 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 formatter to recognise your date string. You can then obtain an NSDate object from this using NSDate *myDate

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

自闭症网瘾萝莉.ら 提交于 2019-11-28 09:57:56
I'm trying to figure out what day (i.e. Monday, Friday...) of any given date (i.e. Jun 27th, 2009) Thank you. 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 the date format string in the setDateFormat: call Lots more information at: http://developer.apple.com