I have such response from server Date = "/Date(1348783200000+0200)/" how can I parse it to timestamp or date (example: Monday 21, September, 2012)?? please help..
It looks like your date is in milliseconds. You will need to divide by 1000 and cast it into an NSDate after that. Then you can just use NSDateFormatter with NSDateFormatterLongStyle to show the date in that format.
NSTimeInterval dateInterval = dateWithMilliseconds;
dateInterval = dateInterval / 1000;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateInterval];
NSDateFormatter *formatter = [NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
NSString *dateString = [formatter stringFromDate:date];
Related
I have one Date in string format "2013-03-19T19:00:50"
I am trying to convert it into NSDate using NSDateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-ddThh:mm:ss"];
NSDate *startDate = [dateFormatter dateFromString:date];
NSLog(#"date in date format : %#",startDate);
but it is giving me null date
date in date format : (null)
What is the issue?
Use :
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
You have time in 24 hour format, so HH is required. hh is used when time is in 12 hour format.
And T is required to be in single quote, T is not a part of date this is an added text on it.
According to the date formatter patterns hh means Hour [1-12] You want Hour [0-23] which is HH.
And any letters that are not date format patterns, or must not be interpreted in this way have to be put in between apostrophes.
use [dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
Do like this,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *startDate = [dateFormatter dateFromString:date];
NSLog(#"date in date format : %#",startDate);
I have a string date in UTC format:
"2012-11-20T05:23:02.34"
now I want the UTC NSDate object for this... and I implemented this function.
+ (NSDate *)utcDateFromString:(NSString *)string withFormat:(NSString *)format {
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
inputFormatter.timeZone =[NSTimeZone timeZoneWithAbbreviation:#"UTC"];
[inputFormatter setDateFormat:format];
NSDate *date = [inputFormatter dateFromString:string];
[inputFormatter release];
return date;
}
and called it like this...
NSDate* submittedDate =[NSDate utcDateFromString:submittedDateString withFormat:#"YYYY-MM-dd'T'HH:mm:ss.SS"];
but this returns the NSDate description as this..
2011-11-20 05:23:02 +0000
Now if you notice there is one year difference in the dates... can anyone explain why is this happening..?
Thanks for your help.
in the dateformmater, use yyyy instead of YYYY
It should be
NSDate* submittedDate =[NSDate utcDateFromString:submittedDateString withFormat:#"yyyy-MM-dd'T'HH:mm:ss.SS"];
That will solve the problem.
In the following link, search for YYYY.
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
It uses yyyy to specify the year component. A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.
try this bellow method..
-(NSDate *)getUTCFormateDate:(NSString *)localDate withFormat:(NSString *)format
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:format];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setTimeZone:timeZone];
NSDate *date = [dateFormatter dateFromString:localDate];
return date;
}
And Use Like bellow..
NSDate *dateHere = [[NSDate alloc]init];
dateHere = [self getUTCFormateDate:#"2012-11-20T05:23:02.34" withFormat:#"yyyy-MM-dd'T'HH:mm:ss.SS"];
NSLog(#"Date here %#",dateHere);
and i get output is Date here 2012-11-20 05:23:02 +0000
see my another answer which return string date with UTF format..
convert NSstring date to UTC dateenter link description here
I have a string that has a date format of HH:mm so for example it could 12:00 or 22:00, and I input that into my NSDateFormatter by setting it as the date format. I just need to construct a custom date. The problem is when I have done this and I get my parsed string as 2012-05-17 12:00:00 +0000 if the date is the 17th of May.
NSDate *today = [NSDate date];
NSDateFormatter *output = [[NSDateFormatter alloc] init];
[output setDateFormat:[NSString stringWithFormat:#"yyyy-MM-d %#:00 +0000",#"12:00"]];
NSString *finalTodayString = [output stringFromDate:today];
parsedDateString = [NSString stringWithString:finalTodayString];
The problem is when I parse it again to just include the HH:mm I get something totally different. For example if I have this code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-d HH:mm ZZZ"];
NSDate *date = [dateFormatter dateFromString:fullDateString];
NSDateFormatter *dateFormmater2 = [[NSDateFormatter alloc] init];
[dateFormmater2 setDateFormat:#"HH:mm"];
NSString *string =[dateFormmater2 stringFromDate:date];
Then the string should be 12:00 but instead it becomes 14:00. Please help.
Remove the Greenwich Meridian time "+0000" from the first code
I have in variable value from PHP function mktime(time from epoch), exacly 133123088.
How I can change it for readable date in iphone?
Is any function to convert this format to another like NSDate or just string ?
NSDate* date = [NSDate dateWithTimeIntervalSince1970: 133123088]
NSTimeInterval timeInterval = 133123088; // NSTimeInterval is a double
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
Notice that 133123088 corresponds to a date in 1974. So I guess you are missing a digit in 133123088 to make it a date in 2012: For example : 1331230880
NSTimeInterval timeInterval = 1331230880;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy/MM/dd HH:mm:ss";
NSString *dateString = [formatter stringFromDate:date];
How can I compare the dates only, not the time. I am using
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *tempDate = #"2-2-2012"; //Dynamic Date
NSDate *dateString = [dateFormatter dateFromString:tempDate];
NSLog(#"%#",dateString);
It logs this: 2012-02-01 18:30:00 +0000
NSDate *now = [NSDate date];//Current Date
NSLog(#"%#",now);
It logs this: 2011-04-04 14:49:45 +0000
I want to compare Dynamic date and current date, I don't need time. I may not using the correct NSDateFormatter. Can anyone of you tell me how to do this? If I am not clear, please let me know.
Suppose I have to strings
date1 = 3-2-2011;
date2 = 4-5-2020;
I want to convert them in date, only after that I can compare them. Its not happening from my date Formatter. Please have a look.
Thanks!
Simplest way is to compare date by converting it into string.
Sample Code is as shown below:
//Current Date
NSDate *date = [NSDate date];
NSDateFormatter *formatter = nil;
formatter=[[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
NSString *dateString = [formatter stringFromDate:date];
[formatter release];
//Other Date say date2 is of type NSDate again
NSString *date2String = [formatter stringFromDate:date2];
//Comparison of Two dates by its conversion into string as below
if([date2String isEqualToString:dateString])
{
//Your logic if dates are Equal
}
else if(![date2String isEqualToString:dateString])
{
//Your Logic if dates are Different
}
EDIT:
Checkout this link.
Comparing dates
http://www.iphonedevsdk.com/forum/iphone-sdk-development/64625-how-compare-2-dates.html
Hope This Helps You. :)
Use NSCalendar and NSDateComponents to get a date components object. Then you can look at only those parts of the date that you care about.
If you're just trying to determine whether two dates are the same, regardless of time, one way to go is to use NSDate's -timeIntervalSinceDate: method. If the time interval returned is less than 86,400 seconds (i.e. 24 hours * 60 minutes * 60 seconds) then you can feel fairly sure that it's the same day. Changes related to such things as daylight savings time and leap seconds introduce some possibility of error... if that's a problem, go with NSDateComponents.
NSDate *date = [NSDate date];
NSDateFormatter *formatter = nil;
formatter=[[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
[formatter setLocale:[NSLocale autoupdatingCurrentLocale]];
NSString *dateString = [formatter stringFromDate:date];
[formatter release];