Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have two NSDate objects
one in this format
yyyy:MM:dd HH:mm:ss
the other one is in
yyyy-MM-dd HH:mm:ss
Is there any difference between them?
I also tried to convert the second format one to the first format, but for some reason, I can get it to the correct NSString but not in NSDate, NSDate always show as yyyy-MM-dd, I cannot find the reason to it.
here is the code i did to convert from - to :
//photo date has this date 2013-07-21 18:02:13
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:photoDate];
dateString = [dateString stringByReplacingOccurrencesOfString:#"-" withString:#":"];
NSDateFormatter *converter = [[NSDateFormatter alloc] init];
[converter setDateFormat:#"yyyy:MM:dd HH:mm:ss"];
NSDate * newPhotoDate = [converter dateFromString:dateString];
// newPhotoDate still has it in 2013-07-21 18:02:13 but dateString actually is in 2013:07:21 18:02:13
NSDate is an date object there is no formatting on the object.Formats comes for getting the string value from NSDate so that we can present in what way we want.What ever format it may be NSDate remains the same
Eg
I/P
NSDate *date=[NSDate date];
NSLog(#"%#",date);
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd-MM-yyyy"];
NSString *dateString =[df stringFromDate:date];
NSLog(#"%#",dateString);
[df setDateFormat:#"dd:MM:yyyy"];
dateString =[df stringFromDate:date];
NSLog(#"%#",dateString);
O/P
2013-07-26 12:47:08.727 Trial[4191:11303] 2013-07-26 07:17:08 +0000
2013-07-26 12:47:08.728 Trial[4191:11303] 26-07-2013
2013-07-26 12:47:08.731 Trial[4191:11303] 26:07:2013
You also need to Convert NSString to NSDate;
Such Like..
NSString *dateString = #"2013-7-12 12:22:51";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *dateFromString = [[NSDate alloc] init];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"Date : %#", date);
Related
This question already has answers here:
Converting NSString to NSDate (and back again)
(17 answers)
Closed 6 years ago.
I am receiving TimeString from server in such format - "2012-04-23T14:00:00.511Z" and for some reason I get nil when trying to convert that into NSDate object by doing following
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss a"];
NSDate *myDate = [df dateFromString: #"2012-04-23T14:00:00.511Z"];
What might be the issue?
Your code should be like,
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *myDate = [df dateFromString: #"2012-04-23T14:00:51Z"];
NSLog(#"%#",myDate);
This question already has answers here:
NSDateFormatter with default time zone gives different date than NSDate description method
(2 answers)
Closed 8 years ago.
NSString *str=#"2015-01-01 00:00:00”;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate *dtConverted = [dateFormatter dateFromString:str];
if (dtConverted==nil) {
dtConverted=nil;
}
return dtConverted;
if my str=2015-01-01 00:00:00 then date return 2014-12-31 18:30:00 +0000
if my str=0000-00-00 00:00:00 then date return null
Please help me to resolve my problem. Need the same date as I do have in string.
NSString *str= #"2015-01-01 00:00:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *dtConverted = [dateFormatter dateFromString:str];
if (dtConverted==nil) {
dtConverted=nil;
}
return dtConverted;
its working perfectly fine and if you want 24 hour format use HH:mm:ss instead of hh:mm:ss
This question already has answers here:
NSDate Format outputting wrong date
(5 answers)
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 9 years ago.
I am trying to convert my date string to NSDate but its return correct date and wrong time.
This is my code :
NSString *dateStr = #"2013-12-20 12:10:40";
// Convert string to date object
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
NSDate *lastUpdatedate = [dateFormatter dateFromString:dateStr];
NSLog(#"lastUpdatedate : %#",lastUpdatedate);
It returns this :
lastUpdatedate : 2013-12-20 06:40:40 +0000
- [NSDate description] (which is called when passing it to NSLog) always prints the date object in GMT timezone, not your local timezone. If you want an accurate string representation of the date, use a date formatter to create a correct string according to your timezone.
As #Leo Natan said, - [NSDate description] always gives date in GMT timezone. If you want to convert into local timezone then use following code.
NSString *dateStr = #"2013-12-20 12:10:40";
// Convert string to date object
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
NSDate *lastUpdatedate = [dateFormatter dateFromString:dateStr];
NSLog(#"lastUpdatedate : %#",[self getLocalTime:lastUpdatedate]);
-(NSDate *) getLocalTime:(NSDate *)date {
NSTimeZone *tz = [NSTimeZone defaultTimeZone];
NSInteger seconds = [tz secondsFromGMTForDate: date];
return [NSDate dateWithTimeInterval: seconds sinceDate: date];
}
OUTPUT:
lastUpdatedate : 2013-12-20 12:10:40 +0000
NSString *dateStr = #"2013-12-20 12:10:40";
NSDateFormatter *dateFormatterTest = [[NSDateFormatter alloc] init];
[dateFormatterTest setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
[dateFormatterTest setLocale:[NSLocale currentLocale]];
NSDate *d = [dateFormatterTest dateFromString:dateStr];
Set NSLocale in your code, and you get perfect result.
You can try this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *lastUpdatedate = [dateFormatter dateFromString:dateStr];
NSTimeInterval sourceGMTOffset = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:lastUpdatedate];
lastUpdatedate = [lastUpdatedate dateByAddingTimeInterval:sourceGMTOffset];
NSLog(#"lastUpdatedate : %#",lastUpdatedate);
This question already has answers here:
NSDate from NSString gives null result
(3 answers)
Closed 9 years ago.
My code is the following:
NSString *dateString = #"1339007317";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSString *year = [dateFormatter stringFromDate:date];
NSLog(#"%#",year);//null
How to fix that? thanx.
It is because you are using unix timestamp and you can convert unix timestamp into year or any date format as :
//Posted Date Format
NSString *dateStr = #"1339007317";
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[dateStr doubleValue]];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en-US"];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy"]; //Here you can set any date format Ex:#"dd MMM, yyyy hh:mm a" or#"dd/MM/yyyyy" according to your requirement
[dateFormatter1 setLocale:locale];
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
dateStr = [dateFormatter1 stringFromDate: date];
Hope it helps you.
You need two date formats. The first should match the format of the string you wish to convert to an NSDate. The second format needs to represent the format you want. Use the 2nd to convert the NSDate to the new string.
Also, why do you create a date object then immediately replace it with a new one?
Change this:
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
to:
NSDate *date = [dateFormatter dateFromString:dateString];
Your code needs to be like this:
NSString *dateString = #"1339007317";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"???"]; // format that matches the dateString
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"yyyy"]; // the desired new format
NSString *year = [dateFormatter stringFromDate:date];
NSLog(#"%#",year);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting NSString to NSDate (and back again)
I make a request to the flickr api and it returns me the date in the following format
"2013-02-01T06:25:47Z"
How do i convert this into NSDate format??
It's a simple one, converting NSString to NSDate we use NSDateformatter using dateFromString method. We need to provide the Dateformatter style with existing style for NSString
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:#"2013-02-01T06:25:47Z"];
NSTimeZone *pdt = [NSTimeZone timeZoneWithAbbreviation:#"PDT"];
[dateFormatter setTimeZone:pdt];
[dateFormatter setDateFormat:#"HH:mm:ss zzz"];
[dateFormatter setDateFormat:#"K:mm a, z"];
NSString * updated String = [dateFormatter stringFromDate:date];
You can use below function:
-(NSDate *)getDateFromString:(NSString *)pstrDate
{
NSDateFormatter *df1 = [[[NSDateFormatter alloc] init] autorelease];
[df1 setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *dtPostDate = [df1 dateFromString:pstrDate];
return dtPostDate;
}
Hope, it will be helpful to you.
This function will accept your string date and return the NSDate value.
Cheers!