I'm trying to get the current date with the following format: dd/MM/yyyy.
The way I would format it, is like this:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
And I would get the current date like this: [NSDate date]. How can I mix the 2 together, and get the current date with that format?
If you want to get current day like format this: dd/MM/yyyy.
You can use this code:
NSDate *datecenter = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"d, MMMM, YYYY"];
NSString *dateString = [dateFormat stringFromDate:datecenter];
lbldaymonthyear.text = dateString;
This is probably the most succinct way, you can use different NSDateFormatterStyles to change the way the date is displayed later
NSString *date = [NSDateFormatter localizedStringFromDate:[NSDate date]
dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterNoStyle];
NSDateFormatterShortStyle
Specifies a short style, typically numeric only, such as “11/23/37” or “3:30 PM”.
Equal to kCFDateFormatterShortStyle.
More styles here: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/#//apple_ref/c/tdef/NSDateFormatterStyle
You can use this code:
NSDate *datecenter = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd, MMMM, YYYY"];
NSString *dateString = [dateFormat stringFromDate:datecenter];
yourlabel.text = dateString;
Related
All,
I want date format in dd/MMM/yyyy format below is the code i am using.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle:#"dd/MMM/yyyy"];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
But in the dateString i am getting in Oct 21,2014 which is diffrent how can i get in "21/Oct/2014" format please let me know
The issue is that you're calling setDateStyle: instead of setDateFormat:
Use this code
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MMM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:[NSDate date]];
NSLog(#"%#", dateString);
This code outputs "22/Oct/2014"
I want to format 06-06-2013 1:51 PM as Jun 06,2013.
I have tried all possible different formatting styles using NSDateFormatter but failed.
Try this
First You need to convert this string back to NSDate then again convert the NSdate to string using formatter.
NSDateFormatter *dateFormatForDB = [[NSDateFormatter alloc] init];
[dateFormatForDB setDateFormat:#"dd-MM-yyyy HH:mm a"]; //Note capital H is 4 24-hour time format
NSDate *aDate = [[[NSDate alloc] initWithTimeInterval:0 sinceDate:[dateFormatForDB dateFromString:aDateString]] autorelease];
if(aDate){
[formatter setDateFormat:#"MMM dd,yyyy"];
NSString *date = [formatter stringFromDate:aDate];
[dateFormatForDB release];
}
Try to use this format
[df setDateFormat:#"MMM dd,yyyy];
By looking at your format, i think this is what you are looking for...
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd-MM-yyyy hh:mm a"];
NSDate *newDate = [df dateFromString:dateString];
NSDateFormatter *df2 = [[NSDateFormatter alloc] init];
[df2 setDateFormat:#"MMM dd,yyyy"];
NSString *formattedDate = [df2 stringFromDate:newDate];
This will give you the date as string in the required format. If you want time in 24hr format, replace 'hh' with 'HH'.
First time caller, long time listener :) Love this site. I'm new to Objective-C but have a blast learning. I'm sorry for such a simple question but I can't figure this out. :(
I have two NSStrings in the format:
itemdate: [March 1, 2013] lastloaddate: [January 1, 1980]
I want to find out which string (date) is most recent. To do this I'm trying to convert the NSString to NSDate and then do a compare but i'm not having any luck. Here is my code. Any and all help would be greatly appreciated!
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd"];
NSDate *titemdate = [df dateFromString: itemdate];
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
[df1 setDateFormat:#"yyyy-MM-dd"];
NSDate *tlasttimerundate = [df1 dateFromString: lastdate];
if ([tlasttimerundate compare:titemdate] == NSOrderedDescending)
{
NSLog(#"itemdate is newer than lastdate");
}
Use [df setDateFormat:#"MMMM d,YYYY"]; instead of [df setDateFormat:#"yyyy-MM-dd"];
The MMMM format will get the full date month where as the MM format will just the months number. For example MM is 5 and MMMMM is May.
To achieve the date compare:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMMM d,YYYY"];
df.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"EN"];
NSDate *titemdate = [df dateFromString: itemdate];
NSDate *tlasttimerundate = [df dateFromString: lastdate];
if ([tlasttimerundate compare:titemdate] == NSOrderedDescending)
{
NSLog(#"itemdate is newer than lastdate");
}
You can find more about the date formats here: http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns
I have datetime values coming into my app from a web service. The format is as follows:
2013-03-24T09:45:00.000-04:00
I need only the time from this value. I am using NSDateFormatter and am setting the date format as follows:
[df setDateFormat:#"HH:mm:ss"];
and when I do this I get a rather cryptic runtime error. However, I have no errors when I use the following (though it doesn't satisfy my requirement):
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.sssz"];
Can someone help?
Try this
NSString *originalDateString = #"2013-03-24T09:45:00.000-04:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.sssz"];
NSDate *date = [dateFormatter dateFromString:originalDateString];
[dateFormatter setDateFormat:#"HH:mm:ss"];
NSString *newDateString = [dateFormatter stringFromDate:date];
Maybe if you use date formater to retrieve a NSDate instance and from there do you own formating.
NSSTring *dateString = "2013-03-24T09:45:00.000-04:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *dateFromWebApp = [dateFormatter dateFromString:dateString];
Haven't tested tho but it should work.
Cheers
I try to convert my NSString to NSDate object, but NSDateFormatter returns me a strange value.
Here is code:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:#"2012-08-15 00:00"];
[dateFormat release];
date value is 2012-08-14 21:00 +0000. It is 3 hours difference between NSString value and NSDate value. I think I've missed something, but I don't know what.
This is what i use:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *date = [dateFormat dateFromString:#"2012-08-15 00:00:00 +0000"];
NSLog(#"\n\n DATE: %# \n\n\n", date);
The +0000 is timezone, so make sure you use your timezone, like +0400.
Edit:
If you can't change the string, you can use this code to do it:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date = [dateFormat dateFromString:#"2012-08-15 00:00"];
As i knew NSDate holds Grinwich time, so if you are in Moscow time zone, everything is wright
In objective c for NSDate if you did not set the setTimeZone, NSDate will take default timezone as localTimeZone. so if you need to get the exact date which you give as NSString string format, you need to setTimeZone as UTC. Follow the sample code, I guess it will be helpful for you.
NSDateFormatter *loacalformatter=[[NSDateFormatter alloc]init];
[loacalformatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *localDate =[loacalformatter dateFromString:#"2012-08-15 00:00"];
NSLog(#"localDate :%#",localDate);
NSDateFormatter *UTCformatter=[[NSDateFormatter alloc]init];
[UTCformatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[UTCformatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *UTCDate =[UTCformatter dateFromString:#"2012-08-15 00:00"];
NSLog(#"UTCDate :%#",UTCDate);
UTCDate :2012-08-15 00:00 +0000 (GMT+00:00)
As suggested in the comments, if the date you receive is UTC then you need to convert it to your local timezone. Apple recommend you always use a properly configured NSDateFormatter when displaying dates, to handle localisation issues.
Here's some example code for turning an NSDate into an NSString:
NSDate *date = // initialised elsewhere
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.locale = [NSLocale currentLocale];
dateFormat.timeZone = [NSTimeZone localTimeZone];
dateFormat.timeStyle = NSDateFormatterShortStyle;
dateFormat.dateStyle = NSDateFormatterShortStyle;
dateFormat.locale = [NSLocale currentLocale];
NSString *dateAsString = [dateFormat stringFromDate:date];