I get the date from URL as GMT Format as follows.
Thu, 13 Nov 2010 05:15:01 GMT
I want to display that date as ordinary format.
I used the following code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH':'mm':'ss"];
NSDate *date = [dateFormatter dateFromString:#"Thu, 13 Nov 2010 05:15:01"];
NSString *strDate = [dateFormatter stringFromDate:date]; //[NSDate date]]; // Ordinary Format
NSLog(#"Date is ........ %#", strDate);
[dateFormatter release];
This above code display the following output.
Date is ........ Sat, 13 Nov 2010 05:15:01
But when I put the date as follow the date "Thu, 13 Nov 2010 05:15:01 GMT" in NSDate then the out put is display as NULL.
How I will edit this
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH':'mm':'ss"];
to this date "Thu, 13 Nov 2010 05:15:01 GMT"
Can't you simply remove the GMT substring from your string and call the dateFromString: on the resulting string?
Related
Here is my code :
[dateFormat setDateFormat:#"EEE MM d HH:mm:ss yyyy"];
logDate = [dateFormat dateFromString:[line substringToIndex:24]];
line consist of string : "Mon Feb 18 09:25:53 2013: FAILED : Configuration-Update :"
I get the date alone "Mon Feb 18 09:25:53 2013" and formatted it with #"EEE MM d HH:mm:ss yyyy" format.
I get an incorrect output : "2013-02-18 03:55:53 -0000" Time alone is printed incorrectly. I followed Date Format Patterns for specifying patters but still I am phasing this issue. I am not able to understand where it going wrong.. It would be helpful if someone finds the problem.
thanks in advance
Try this one
NSDate *date = [NSDate date];
NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"EEE MM d hh:mm:ss yyyy"];
NSLog(#"%#",[formatter stringFromDate:date]);
NSLog(#"%#",date);
When you just log using NSLog you get the UTC format (UTC consists of offset value, in India its standard time +5.30)that is yyyy-MM-d HH:mm:ss z , z is the offset added or subtracted to or from current time based on the locality.
For example
NSLog(#"%#",[formatter stringFromDate:date]);
NSLog(#"%#",date);
Tue 02 26 05:38:00 2013 //"EEE MM d hh:mm:ss yyyy"
Tue 02 26 17:46:22 2013 //"EEE MM d HH:mm:ss yyyy"
2013-02-26 12:08:00 +0000 //just logging using NSLog
Hope this helps you
You'll need to set timezone for a NSDateFormatter when you print resulting date like this:
[formatter setTimeZone:[NSTimeZone localTimeZone]];
You need to use as :
NSString *line=#"Mon Feb 18 09:25:53 2013: FAILED : Configuration-Update :";
NSDateFormatter *dateFormat=[NSDateFormatter new];
[dateFormat setDateFormat:#"EEE' 'MM' 'd' 'HH:mm:ss' 'yyyy"];
NSDate *logDate = [dateFormat dateFromString:[line substringToIndex:24]];
NSLog(#"logDate->%#",logDate);
If we take the log of the NSdate object that will show the date with respect to the timezone +0000 only. for that we need to format the date to our local timezone. like this..
[dateFormat setDateFormat:#"EEE MM d HH:mm:ss yyyy"];
logDate = [dateFormat dateFromString:#"Mon Feb 18 09:25:53 2013"];
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
NSLog(#"Date %#",[dateFormat stringFromDate:logDate]);
thanks to all.. it worked by changing the code this way ..
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init]autorelease];
[dateFormat setDateFormat:#"EEE MM d HH:mm:ss yyyy"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
return [dateFormat dateFromString:dateString];
How to get this format of date from NSString;
Wed, 22 Jun 2011 12:36:00 +0100 to Wed, 22 Jun 2011.
Thanks
Try this code.
NSString *dateStr = #"Wed, 22 Jun 2011 12:36:00 +0100";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE, d MMM yyyy HH:mm:ss ZZZ"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"EEE, MMM d YYYY"];
dateStr = [dateFormat stringFromDate:date];
NSLog(#"Date -- %#",dateStr);
[dateFormat release];
The minimalistic version is E, d MMM y or to specify 2 digit days and 4 digit years E, dd MMM yyyy. The Date Formatter uses the Unicode Technical Standard #35.
Hi I have a string that is like this: Thu, 09 Jun 2011 5:00 pm CST, where CST could be any time zone or something.
Help I tried this already but it's null:
NSDateFormatter *formatter2 = [[NSDateFormatter alloc]init];
[formatter2 setDateFormat:#"EEE, dd MMM yyyy h:mm z"];
[formatter2 setLocale:[[[NSLocale alloc]initWithLocaleIdentifier:#"en_US_POSIX"]autorelease]];
[formatter2 setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSDate *time = [formatter2 dateFromString:#"Thu, 09 Jun 2011 5:00 pm KST"];
[formatter2 release];
NSLog(#"%#",time); //null
Am I doing somethings wrong? Help me I'm new.
You aren't accounting for the AM/PM. I believe that that is 'a'.
try:
NSDateFormatter *formatter2 = [[NSDateFormatter alloc]init];
[formatter2 setDateFormat:#"EEE, dd MMM yyyy h:mm a z"];
[formatter2 setLocale:[[[NSLocale alloc]initWithLocaleIdentifier:#"en_US_POSIX"]autorelease]];
[formatter2 setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSDate *time = [formatter2 dateFromString:#"Thu, 09 Jun 2011 5:00 pm KST"];
[formatter2 release];
NSLog(#"%#",time); //null
Hi Im trying to convert an NSString to NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZZ"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:lastModified];
But when I do a
NSLog(#" date: %# ", dateFromString);
I get date: (null)
The lastModified string has date in string format as "Tue, 15 Feb 2011 13:30:42 GMT"
What am I doing wrong?
your input (lastModified) does not match the specified date format.
a properly formed string representation of the time specified by lastModified for the date format is:
"2011-02-15 13:30:42 GMT", but you're passing "Tue, 15 Feb 2011 13:30:42 GMT".
I have the date that is in format 2010-11-17.This is of the form NSString.
I want to convert this NSString date into format 17 Nov 2010 and display in a label
This date is just not the current date but may even be the older dates.
So I cant use the [NSDate date] instance to get the date.
I tried using NSDateFormatter with the method dateFromString.
But it gives me a null value.
Here is the code snippet
NSString *dates = #”2010-11-16”;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM yyyy"];
NSDate *dates1 = [dateFormatter dateFromString:dates];
NSString *dates2 = [dateFormatter stringForObjectValue:dates1];
NSLog(#"dates : %#",dates);
NSLog(#"Dates 2 : %#",dates2);
[dateLabel setText:dates2];
NSLog(#"Formatted Date is : %#",dateLabel.text);
What should be done?
Please Help and suggest.
Thanks.
Try this
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:#"yyyy-MM-dd"];
NSDate *date2 = [formater dateFromString:#"2010-11-17"];
[formater setDateFormat:#"d MMM yyyy"];
NSString *result = [formater stringFromDate:date2];
NSLog(#"RESULT %#",result);
I got the result.Correct me if this approach is wrong.
Here are some formats for who desire to know!!
12:16:45 PM on January 01, 2000 --> hh:mm:ss a 'on' MMMM dd, yyyy
Wednesday, Sep 12, 2018 --> EEEE, MMM d, yyyy--
09/12/2018 --> MM/dd/yyyy--
09-12-2018 14:11 --> MM-dd-yyyy HH:mm--
Sep 12, 2:11 PM --> MMM d, h:mm a--
September 2018 --> MMMM yyyy--
Sep 12, 2018 --> MMM d, yyyy
Wed, 12 Sep 2018 14:11:54 +0000 --> E, d MMM yyyy HH:mm:ss Z
2018-09-12T14:11:54+0000 --> yyyy-MM-dd'T'HH:mm:ssZ
12.09.18 --> dd.MM.yy
10:41:02.112 --> HH:mm:ss.SSS
12:16:45 PM --> hh:mm:ss a
AM or PM --> a
NSDateFormatter uses date format patterns from UNICODE.
So for your 17 Nov 2010 date you need something like this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"d MMM yyyy"];
NSString *result = [formatter stringForObjectValue:date];
Where date variable contains the date you'd like to format.
UPDATE: Try changing your code to this:
NSString *dates = #”2010-11-16”;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *dates1 = [dateFormatter dateFromString:dates];
[dateFormatter setDateFormat:#"dd MMM yyyy"];
NSString *dates2 = [dateFormatter stringForObjectValue:dates1];
NSLog(#"dates : %#",dates);
NSLog(#"Dates 2 : %#",dates2);
[dateLabel setText:dates2];
NSLog(#"Formatted Date is : %#",dateLabel.text);