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.
Related
trying to convert NSString to NSDate
NSString *startDateForCal='09-03-2016 08:00:00 AM'
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"dd-MM-yyyy HH:mm:ss a"];
NSDate * EventStart = [dateFormatter dateFromString: startDateForCal];
But getting mixed result as "2016-03-09 18:30:00 +0000" returning date as correct but time didn't match with string It would be welcome any suggestions
Thank you
it should be
[dateFormatter setDateFormat: #"dd-MM-yyyy hh:mm:ss a"];
i.e. no capital HH for hour. HH return the Hour in 24 format
For more details Click now
and regarding the error in date,
since you have set the time format wrong( as HH instead of hh), it took the time as 12:00 AM and showed it in GMT timezone(IST - 5 and half hours, so in your case 12:00 AM - 5:30 = 18:30 of previous day), i guess you haven't set the locale properly.
setting the locale(in swift),
dateFormatter.locale = NSLocale(localeIdentifier:"en_IN")
Your question makes no sense. Your goal was to obtain an NSDate from an NSString. You did that, and you did it successfully.
How you discover what that NSDate is, is a completely different matter. It is represented to you by default (in the console) in a certain time zone and format. But that is irrelevant; it is still the same date, and it is the correct date.
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]);
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.
I want to get the date and time in a specific time zone. I am getting most of the things right but just at the end when i get the date from NSString using NSDateFormatter method it returns me the date in the GMT specific time zone. The method [formatter stringFromDate:gmtDate]; return me the expected date and time. The problem happen when i get the date from the string i-e when i execute this method self.localTime = [formatter dateFromString:str];. self.localTime is a NSDate property in my class.
So when i print the str it gives me the date and time in that specific time zone which is represented as self.timeZoneID, which is also a property on my class
NSDate *gmtDate = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:self.timeZoneID]];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *str = [formatter stringFromDate:gmtDate];
NSLog(#"Date string : %#", str);
self.localTime = [formatter dateFromString:str];
Any idea that what could be the reason that i am getting the right string output but when i assign it to my property localTime it give me the time in GMT
Reason is that NSDate have a default time zone that is GMT for consideration and when a user wants to have time specific for some time zone then NSDateFormatter provides way to set specific time zone which you are using for gmtDate object and not for self.localTime(this is taking GMT ,default time zone).
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.