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];
Related
This question already has an answer here:
NSDateFormatter show wrong year
(1 answer)
Closed 6 years ago.
My code:
NSString *dateStr = #"03-02-2017";//[responseObject objectForKey:#"event_date"];
NSLog(#"'%#'", dateStr);
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_HK"];
[dateFormat setLocale:locale]; //To fix the format into something like: 10 Feb 2017, but not: 10 2月 2017
[dateFormat setDateFormat:#"dd-MM-YYYY"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"'%#'", date);
// Convert date object to desired output format
[dateFormat setDateFormat:#"dd MMM YYYY"];
dateStr = [dateFormat stringFromDate:date];
NSLog(#"'%#'", dateStr);
And in log it returns
'03-02-2017'
'2016-12-24 16:00:00 +0000'
'25 Dec 2016'
Anyone know why this happened? I searched in google that most cases are causing by timezone. However, I'm pretty sure it's not about timeZone because the difference of dates are too large, but I can't figure out the root of this problem.
I think your dateformat is wrong.
It should be
dd-MM-yyyy
I found that my format is wrong from answer objective-c - NSDateFormatter returns wrong date
and reference by http://nsdateformatter.com .
It should be "yyyy" instead of "YYYY"
I return a string from my database and value is '04/27/2016 1:16pm'. This value is already in UTC.
Now I want to convert that string to NSDATE, keeping it in UTC. When I try to convert string to date, the time is actually moving by 1 hour.
This is how I am doing it
NSString *tuploadtime = [tempDictionary valueForKey:#"uploadTime"];
//date conversions
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *duploadtime = [[NSDate alloc] init];
duploadtime = [dateFormatter dateFromString:tuploadtime];
NSLog(#"tuploadtime=%#, duploadtime=%#", tuploadtime, duploadtime);
the result is coming back as 2016-04-27 12:16:01 UTC.
result is
2016-04-27 10:12:44.612 x[1558:48608] tuploadtime=4/27/2016 2:10:33 PM, duploadtime=2016-04-27 12:10:33 +0000
basically the time is moving back 1 hour but I want to keep it the same.
hope I am making sense
Proper String Format for Date is most Important.
There are some methods to have HOURS format as follow with their differences..
kk: will return 24 format Hour in (01-24) hours will (look like 01, 02..24).
HH will return 24 format Hour in (00-23) hours will(look like 00, 01..23).
hh will return 12 format Hour (look like 01, 02..12).
so you should use your code like
NSString *tuploadtime = [tempDictionary valueForKey:#"uploadTime"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy hh:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *duploadtime = [[NSDate alloc] init];
duploadtime = [dateFormatter dateFromString:tuploadtime];
NSLog(#"tuploadtime=%#, duploadtime=%#", tuploadtime, duploadtime);
for More Date format refer this link
Your date format string is inconsistent:
[dateFormatter setDateFormat:#"MM-dd-yyyy HH:mm:ss a"];
HH means to use a 24-hour format. But then you also used a to indicate AM/PM. Using both is confusing the formatter and giving you off-by-one. You meant to use hh here.
In my application I am getting an NSString with the date as 01-22-12(MM-dd-yy). Now I want to convert that string into an NSDate. I used the code below. But it is giving the date as 2012-01-04 05:00:00 +0000
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yy"];
NSDate *dateFromString = [dateFormatter dateFromString:self.selectedWeek];
What is the proper way to convert my string to an NSDate?
From your original question I'm not totally clear whether you want:
1 the string as an NSDate.
or
2 to be able to get the original date NSString back out of an NSDate instance.
Your code is basically right. I just ran a slightly adapted version of it, which seemed to work fine:
NSString *dateString = #"01-22-12";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yy"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSString *outString = [dateFormatter stringFromDate:date];
NSLog(#"%#",date);
NSLog(#"%#",outString);
This produced:
2013-02-24 09:57:24.352 datesAgain.m.out[787:707] 2012-01-22 00:00:00 +0000
2013-02-24 09:57:24.352 datesAgain.m.out[787:707] 01-22-12
So, either way the result seems to be what you were looking for. I suspect that the reason you are getting an incorrect value is that self.selectedWeek doesn't have the value you think it does. I'd inspect it, either in the debugger or with NSLog. If you are creating it somewhere else using another format string, be aware that they can be tricky and slightly unintuitive - for instance s means seconds, but S means fractions of a second.
Documentation available here - most recent Unicode formatting standard here (ios6.0/OSX 10.8) - also linked to in previous link, as are all previous relevant standards
you can use descriptionWithCalendarFormat:timeZone:locale:
NSString * mydate = [dateFromString descriptionWithCalendarFormat:#"%Y-%m-%d"
timezone:nil
locale:nil];
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];
My dateFromString is not working and i'm not sure why
NSString *purchase = #"2011-09-30 17:47:57";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [dateFormat dateFromString:purchase];
date is 'invalid CFStringRef'
See anything i might be overlooking?
Try changing the hours in the formatter to capitals, i.e. yyyy-MM-dd HH:mm:ss
I'm not sure if that will solve your error, but that is the correct way to parse a 24 hour time
in the format
"hh" means Hour [1-12].
"HH" means Hour [0-23].
See UTS Date Field Symbol Table for the date format specifiers.
Try this:
NSString *purchase = #"2011-09-30 17:47:57";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:purchase];
Date Formatters Guide - HH is used for 24 hour times, hh for 12 hour.
If you mean that "date" is not accepted as an NSString by another function, that's because it's not a string, it's an NSDate.
(Where, precisely, are you getting the error message, and what is the full text?)