issue while constructing NSDate [duplicate] - ios

This question already has answers here:
NsDate formatter giving wrong Date from String [duplicate]
(2 answers)
Closed 7 years ago.
I am making NSDate from this string but Every time I am getting Previous Month from Selected Month
I am not able to find the bug:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
[dateFormatter setDateFormat:#"dd/MMM/yyyy"];
NSDate *date = [dateFormatter dateFromString:strDate];
NSLog(#"------Date----%#",date);
I have tried with many Datestrings some are with Logs are:
strDate =---- 01/Jan/2016
------Date----2015-12-31 18:30:00 +0000
strDate =----01/Dec/2015
------Date----2015-11-30 18:30:00 +0000

As viking says, what you are seeing is an artifact of the way you are displaying your resulting date. When you log an NSDate object in NSLog, it is always displayed in UTC. If your date does not have a time component, midnight is assumed. If your time zone is AFTER UTC, midnight in your time zone will be displayed as the previous day.
I suggest you create a display date formatter to display your resulting dates, and use that:
- (NSString *) displayStringForDate: (NSDate *) date
{
static NSDateFormatter *dateFormatter;
if (dateFormatter == nil)
{
//This date formatter will default to the current time zone, like you want.
dateFormatter = [[NSDateFormatter alloc]init];
//Adjust the date format as desired
{
return [dateFormatter stringFromDate: date];
}
Then use that method in your test code:
NSLog(#"------Date----%#", [self displayStringForDate: date]);

Related

NSDateFormatter showing wrong date in ios? [duplicate]

This question already has answers here:
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 6 years ago.
i have NSString with this date format "2016-03-16" and i added following code to get the same date in proper NSDate format but its returning " 2016-03-15 18:30:00 +0000 ". How do do i get same "2016-03-16" in NSDate ?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd";
NSString *datePart = [dateFormatter stringFromDate:dateFromString];//2016-03-16
NSDate *dateValue = [dateFormatter dateFromString:datePart];
NSLog(#"%#----",dateValue); //" 2016-03-15 18:30:00 +0000
Your local time zone is presumably UTC+5:30. You are specifying a date but not a time, so the time is implied to be midnight. Midnight on the 16th in your local time zone is 18:30 the day before (the 15th) in UTC time, which is why you get "2016-03-15 18:30:00 +0000"
When you log the date with NSLog(#"%#----",dateValue) you are actually invoking [dateValue description], which displays the date using UTC.
You can use NSLog(#"%#----",[dateValue descriptionWithCalendarFormat:nil timeZone:[NSTimeZone localTimeZone] locale:nil]) and you will see the date in your current time zone.
Be aware though that the description and associated methods such as descriptionWithCalendarFormat methods are only for debugging, you should use an NSDateFormatter to convert dates to strings. iOS_Binod's answer shows one way you could do this.
You try to print NSDate instance in console. That's reason your code print default format value in console.
You need to get string value from NSDate instance with the help of this method [your_dateformater stringFromDate:dateInstance]
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd";
NSString *datePart = #"2016-03-16";
NSDate *dateValue = [dateFormatter dateFromString:datePart];
NSLog(#"string convert into Date is - %#",[dateFormatter stringFromDate:dateValue]);
The date formatter uses your local time zone by default. The -[NSDate description] method (which is what %# calls) uses UTC. This is why the strings are different.

NSDate - dateFromString is returning nil

I have an NSString that has a date. I'm trying to convert that date to an NSDate. When I do that I get nil fro the NSDate. Here is my code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"dateString = %# date = %#", dateString, date);
Here is the output of the NSLog:
dateString = 2015-06-16 date = (null)
What am I doing wrong, and how can I fix it?
The date format does not match the date string. It needs to be: :#"yyyy-MM-dd"
The order and other characters need to match.
yyyy for a four digit year
MM for a two digit month
dd for a two digit day
See: ICU Formatting Dates and Times
Note: NSLog() uses the NSDate description method which presents date/time referenced to GMT (UTC) and NSDateFormatter defaults to your timezone so the date displayed may be different.

Getting one date less [duplicate]

This question already has answers here:
NSDate Format outputting wrong date
(5 answers)
Closed 8 years ago.
I don't why i'm getting one date less, when I'm converting a string from a date, i'm getting one date less, e.g. when i'm converting 18/06/2014, i'm getting 2014-06-17, Any idea why this problem, my codes are:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSDate *date = [dateFormatter dateFromString:#"18/06/2014"];
This is what I'm getting wholly from the log: 2014-06-17 20:00:00 +0000
You will have to take the timezone into account. Your current timezone seems to be ahead of GMT. If you print the entire date with say a time stamp, then you will get the difference. So i suggest you add the timezone to the NSDateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *date = [dateFormatter dateFromString:#"18/06/2014"];
NSLog(#"Date : %#", date);

iOS 7: Convert NSDate to string with custom timezone and get back new NSDate

I wish to modify current system time (set a custom NSTimeZone) and get back a new NSDate object.
The code I've made
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSLog(#"System time: %#", [dateFormatter stringFromDate:[NSDate date]]);
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"Asia/Aqtobe"]];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"Aqtobe time: %#", dateString);
The output
System time: 2014-02-05 10:00:46 +0000
Aqtobe time: 2014-02-05 15:00:46 +0500
But if I try to get new NSDate object from Aqtobe time:
NSLog(#"New NSDate: %#", [dateFormatter dateFromString:dateString]);
I get
New NSDate: 2014-02-05 10:02:40 +0000
Where I was wrong? Thanks in advance
NSDate always returns date in GMT +0:00.
So, it (NSDate object) always have correct converted value but in GMT +0:00.
So for using it as text you will always have to use same date formatter with same zone.
If you want to use date as string from date in other places (out of dateformatter object scope), it is better to make special method for conversion.
It is explained clearly below with example:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss Z";
NSLog(#"System time: %#", [dateFormatter stringFromDate:[NSDate date]]);
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"Asia/Aqtobe"]];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"Aqtobe time: %#", dateString);
// date will always contain value in GMT +0:00
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"New NSDate (NSDate): %#", [dateFormatter stringFromDate:date]);
// converts date into string
NSLog(#"New NSDate (NSString): %#", [dateFormatter stringFromDate:date]);
For detailed explaination you can refer this question: Does [NSDate date] return the local date and time?
NSDate has many methods for comparing date such as isEqualToDate: (for a more detailed of the methods and usage you can see this SO question).
This method, that I took from here, will let you determine if date is between firstDate and lastDate.
- (BOOL)isDate:(NSDate *)date inRangeFirstDate:(NSDate *)firstDate lastDate:(NSDate *)lastDate {
return [date compare:firstDate] == NSOrderedDescending &&
[date compare:lastDate] == NSOrderedAscending;
}
I hope this is what you meant. If not please explain exactly what you're trying to do.
You can use NSDateComponents and NSCalendar to create a new date. From the Date and Time Programming Guide
Creating Dates with Time Zones
Time zones play an important part in determining when dates take
place. Consider a simple calendar application that keeps track of
appointments. For example, say you live in Chicago and you have a
dentist appointment coming up at 10:00 AM on Tuesday. You will be in
New York for Sunday and Monday, however. When you created that
appointment it was done with the mindset of an absolute time. That
time is 10:00 AM Central Time; when you go to New York, the time
should be presented as 11:00 AM because you are in a different time
zone, but it is the same absolute time. On the other hand, if you
create an appointment to wake up and exercise every morning at 7:00
AM, you do not want your alarm to go off at 1:00 PM simply because you
are on a business trip to Dublin—or at 5:00 AM because you are in Los
Angeles.
NSDate objects store dates in absolute time. For example, the date
object created in Listing 16 represents 4:00 PM CDT, 5:00 EDT, and so
on.
Listing 16 Creating a date from components using a specific time zone
NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar
[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"CDT"]];
NSDateComponents *timeZoneComps=[[NSDateComponents alloc] init]; [timeZoneComps setHour:16];
//specify whatever day, month, and year is appropriate
NSDate *date=[gregorian dateFromComponents:timeZoneComps];
If you need to create a date that is independent of timezone, you can store the
date as an NSDateComponents object—as long as you store some reference
to the corresponding calendar.
In iOS, NSDateComponents objects can contain a calendar, a timezone,
and a date object. You can therefore store the calendar along with the
components. If you use the date method of the NSDateComponents class
to access the date, make sure that the associated timezone is
up-to-date.
Anyway, keep in mind that a date is a unique point in time. What you display to the user is different based on their locale and time zone.

Deviation from expected output with NSString to NSDate conversion using NSDateFormatter

I am using the below method to convert a NSString to NSDate.
Always when I construct the NSDate from String, the date is one day behind the current day I have provided as part of the input and hour is 18:30:00 +0000. Why this deviation from what I have provided. I was expecting to have the same date what I have provided and hour as 00:00:00 +0000
+(NSDate*)convertStringToNSDate:(NSString*)string withFormat:(NSString*)format{
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
//[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
[dateFormat setDateFormat:format];
NSDate *date = [dateFormat dateFromString:string];
[dateFormat release];
return date;
}
This question comes up quite regularly but I could not find a suitable duplicate (searching on the phone does not help).
NSDate represents a specific point in time. When you log the value of an NSDate it is displayed in GMT, which is 5.5 hours behind your timezone (India, I assume). So the value is correct. If you run that date back through your date formatter you will get the local time of midnight again, since the date formatter is using your local time zone.

Resources