NSDate issue.. Incorrect time? [duplicate] - objective-c

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
NSDate not returning correct date
when I try to display current time like so:
NSDate *mydate = [NSDate date];
I get this result:
2012-11-24 09:27:13.194 myApp[5284:c07] mydate = 2012-11-24 07:27:13 +0000
the time is off by two hours although the simulator time and OS X time is different. How can I fix that? Thank you in advance..

You need to print out the date with an NSDateFormatter. NSDate doesn't have any timezone information attached to it. Make sure to attach a timezone to your date formatter.
NSDate* now = [NSDate date];
// GET A DATE FORMATTER AND SET THE TIMEZONE
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.timeZone = [NSTimeZone systemTimeZone];
// CHANGE FORMAT TO SUIT YOUR NEEDS
[df setDateStyle: NSDateFormatterFullStyle];
[df setTimeStyle: NSDateFormatterFullStyle];
// GET YOUR FORMATTED DATE STRING
NSString *dateString = [df stringFromDate: now];

Related

Convert Date( 934146000000+0300) into NSString [duplicate]

This question already has an answer here:
NSDate from stange looking string
(1 answer)
Closed 9 years ago.
I get the birthday by parsing the xml. In
-(void)parser:(NSXMLParser*)parser didEndElement
method ,I convert the value of the <birthday></birthday> element into NSDate but it returns /Date(934146000000+0300)/:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:currentStringValue];
How can I convert it into NSDate to be able to converting into NSString?
It looks like you're retrieving the value from a long var.
It currently refers to Mon, 29 Nov 31571 00:00:00 GMT
If you remove three 0s from the end it makes sense...
934146000 = Sun, 08 Aug 1999 21:00:00 GMT
You also need to fix the formatting. The format dd-MM-yyyy will not be able to understand 934146000.
You can do something like...
CGFloat dateValue = 934146000;
NSDate *theDate = [NSDate dateWithTimeIntervalSince1970:dateValue];
This should get the correct date.
But like I said, you need to sort out the value you are getting first.
Seems you are getting the value from Java or C#, as current time in millisecond.
In Objective C, NSDate expect the time as second, not in millisecond. So you have to divide it by 1000.
NSDate * dateOfBirth = [NSDate dateWithTimeIntervalSince1970:[recvDate floatValue]/1000.0];
You can use something like below, but you still need correction for the millisecond to second value.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"EEE, dd MMM yyyy HH:mm:ss ZZZ";
NSDate * date = [formatter dateFromString:str];

NSDate and NSDateFormatter inconsistent [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
NSDateFormatter not giving me correct
I am using a NSDate which I convert to only the date using the following technique:
+ (NSDate *) dateOnly:(NSDate *)date
{
long time = [date timeIntervalSinceReferenceDate];
long timeMod = time % kNumSecondsInDay;
NSTimeInterval newTime = time - timeMod;
return [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:newTime];
}
This is giving me expected results:
NSLog(#"Due Date:%#", _dueDate);
--> Due Date: 2012-11-21 00:00:00 +0000
However, when I use the date formatter, it gives me one day previous:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
NSString *result = [formatter stringFromDate:_dueDate];
NSLog(#"Result: %#", result);
--> Result: 11/20/12
What gives?
Time zone. The date is midnight on 11/21, UTC; if your time zone is behind UTC, then it'll be 11/20.

How to get a local formatted date patterned string in iOS? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to determine if locale's date format is Month/Day or Day/Month?
I've struggled some time now and I'm stuck! I either need a local formatted pattern date string in the form "mm-dd-yyyy" or "08-25-2012". How should I do this? Use dateFormat? Please help me!
NSDateFormatter has a convenience class method:
[NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterNoStyle];
You can specify NSDateFormatterNoStyle,..ShortStyle,..MediumStyle, or..LongStyle
You can use NSDateFormatter with MM-dd-yyyy and then use NSTimeZone applied to the formatter to adjust for the local time zone.
This:
NSDate *date = [[NSDate alloc] init];
NSLog(#"%#", date);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy hh:mm"];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"%#", dateString);
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
[dateFormatter setTimeZone:timeZone];
NSLog(#"adjusted for timezone: %#", [dateFormatter stringFromDate:date]);
Outputs:
2012-08-26 08:30:53.741 Craplet[2585:707] 2012-08-26 12:30:53 +0000
2012-08-26 08:30:53.742 Craplet[2585:707] 08-26-2012 08:30
2012-08-26 08:30:53.743 Craplet[2585:707] adjusted for timezone: 08-26-2012 08:30

current Date and Time - NSDate

I need to display the current Date and Time.
I have used ;
NSDate *currentDateNTime = [NSDate date];
I want to have the current date and time (Should display the system time and not GMT time).
The output should be in a NSDate format and not NSString.
for example;
NSDate *currentDateNTime = [NSDate date];
// Do the processing....
NSDate *nowDateAndTime = .....; // Output should be a NSDate and not a NSString
Since all NSDate is GMT referred, you probably want this:
(don'f forget that the nowDate won't be the actual current system date-time, but it's "shifted", so if you will generate NSString using NSDateFormatter, you will see a wrong date)
NSDate* currentDate = [NSDate date];
NSTimeZone* currentTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSTimeZone* nowTimeZone = [NSTimeZone systemTimeZone];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:currentDate];
NSInteger nowGMTOffset = [nowTimeZone secondsFromGMTForDate:currentDate];
NSTimeInterval interval = nowGMTOffset - currentGMTOffset;
NSDate* nowDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:currentDate];
Every moment in time is the same moment in time everywhere around the world —- it is just expressed as different clock times in different timezones. Therefore, you can't change the date to some other date that represents the time in your timezone; you must use an NSDateFormatter that you feed with the timezone you are in. The resulting string is the moment in time expressed in the clock time of your position.
Do all needed calculations in GMT, and just use a formatter for displaying.
Worth reading
Does [NSDate date] return the local date and time?
Some useful resources for anyone coming to this more recently:
Apple date and time programming guide do read it if you're doing anything serious with dates and times.
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html#//apple_ref/doc/uid/10000039i?language=objc
Useful category on NSDate with lots of utilities does allow a ~new~ date to be generated based on an existing date.
https://github.com/erica/NSDate-Extensions
There's also a swift version of the category
https://github.com/erica/SwiftDates
You need an NSDateFormatter and call stringFromDate this method to get a string of your date.
NSDateFormatter *dateformater = [[NSDateFormatter alloc] init];
[dateformater setDateFormat:#"yyyyMMdd,HH:mm"];
NSString *str = [dateformater stringFromDate: currentDateNTime];
use this method
-(NSDate *)convertDateToDate:(NSDate *) date
{
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSDate *nowDate = [[[NSDate alloc] init] autorelease];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[formatter setDateFormat:#"yyyy-MM-d H:m:s"];
NSString * strdate = [formatter stringFromDate:date];
nowDate = [formatter dateFromString:strdate];
return nowDate;
}
this may return you what you want.
i hope you this may help you.

Struggling to store a date for later use

Hi I'm very new to iOS programming and am playing around with dates (todays date and a date 1 year from now).
Here's the code i'm dabbling with.
NSCalendar * calendar = [NSCalendar currentCalendar];//Create a calendar
NSDate *todaysDate = [[NSDate alloc]init]; //get todays date
NSString *dateToday = [NSString stringWithFormat:#"%#",todaysDate];//convert it to a string
NSDateFormatter *df = [[NSDateFormatter alloc] init];// create a formatter
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ" ];//input how the date looks as a string
myDate = [df dateFromString: dateToday];// change it back to a proper NSDate
NSDateComponents *components = [[NSDateComponents alloc] init]; // create the components
[components setYear:1];//add 1 year
nextYear = [calendar dateByAddingComponents:components toDate:todaysDate options:0]; // build the year from component into a variable
dateNextYear = [NSString stringWithFormat:#"%#",nextYear];//convert it to a string
NSDateFormatter *yearFormat = [[NSDateFormatter alloc] init];// create a formatter
[yearFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ" ];//input how the date looks as a string
myDateInTheFuture = [yearFormat dateFromString: dateNextYear];// change it back to a proper NSDate
NSLog(#" next years date is %# ", myDateInTheFuture);
[yearFormat release];
[components release];
[todaysDate release];
I can get the current date and the future date 1 year from now, but i'm unsure how i would store the future date for comparison, i know i can use the "compare" item for NSDate to check, but when i set the future date to a variable every time it runs it stays relative 1 year apart from what i'm checking it against which is todays date.
Its 3am where i am and my brain is mush so apologises in advance if this is the simplest thing ever and i just can't see it.
Its my first post so go easy on me please.
Thanks
I am not entirely sure what you are trying to do, but this is what I gather:
You want to take the current date, add a year to it, and manipulate the resulting date.
Please notify me if this is not correct.
For this, try the following:
NSDate *todayDate = [NSDate date]; //Create a date that is set to today
NSDate *resultingDate = [calendar dateByAddingTimeInterval:31556926; //Take the current date and add the amount of seconds in a year to it
If you want to store this permanently, use the NSUserDefaults:
to set:
[userDefaults setObject:resultingDate forKey:#"storedDate"];
Hope this helps,
HBhargava
to get:
NSDate *returnedDate = [userDefaults dateForKey:#"storedDate"];