I am working on an app where I need to create a weekly calendar. For this I will need days and dates of current week(Sun Sep30, Mon Oct 1, Tue Oct2 etc.). Can someone please guide me how to achieve this?
Regards
Pankaj
Here is a simple function which extracts date for all the days in this week, staring from Sunday this week to Saturday,
func formattedDaysInThisWeek() -> [String] {
// create calendar
let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)!
// today's date
let today = NSDate()
let todayComponent = calendar.components([.Day, .Month, .Year], fromDate: today)
// range of dates in this week
let thisWeekDateRange = calendar.rangeOfUnit(.Day, inUnit:.WeekOfMonth, forDate:today)
// date interval from today to beginning of week
let dayInterval = thisWeekDateRange.location - todayComponent.day
// date for beginning day of this week, ie. this week's Sunday's date
let beginningOfWeek = calendar.dateByAddingUnit(.Day, value: dayInterval, toDate: today, options: .MatchNextTime)
var formattedDays: [String] = []
for i in 0 ..< thisWeekDateRange.length {
let date = calendar.dateByAddingUnit(.Day, value: i, toDate: beginningOfWeek!, options: .MatchNextTime)!
formattedDays.append(formatDate(date))
}
return formattedDays
}
func formatDate(date: NSDate) -> String {
let format = "EEE MMMdd"
let formatter = NSDateFormatter()
formatter.dateFormat = format
return formatter.stringFromDate(date)
}
If you call method formattedDaysInThisWeek(), the output is like this,
["Sun Oct11", "Mon Oct12", "Tue Oct13", "Wed Oct14", "Thu Oct15", "Fri Oct16", "Sat Oct17"]
Do you just need it for this week? If so, here's some code that you should be able to use:
- (NSString *)stringOfDatesOfThisWeek {
NSArray *datesOfThisWeek = [self dateOfTheWeek:[NSDate date]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE MMM d"];
NSMutableString *dateStringForThisWeek = [[NSMutableString alloc] initWithString:[formatter stringFromDate:[datesOfThisWeek firstObject]]];
for (int i = 1; i < datesOfThisWeek.count; i++) {
[dateString appendFormat:#", %#", [formatter stringFromDate:[datesOfThisWeek objectAtIndex:i]]];
}
}
- (NSArray *)datesOfTheWeek:(NSDate *)todaysDate {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger weekNumber = [[calendar components: NSWeekCalendarUnit fromDate:todaysDate] week];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comp = [gregorian components:NSYearCalendarUnit fromDate:todaysDate];
[comp setWeek:weekNumber]; //Week number.
NSMutableArray *dates = [NSMutableArray new];
for (int i = 0; i < 7; i++) {
[comp setWeekday:1]; //First day of the week. Change it to 7 to get the last date of the week
NSDate *resultDate = [gregorian dateFromComponents:comp];
[dates addObject:resultDate];
}
return dates;
}
and after typing all of that without Xcode, I realized that this question was asked about Swift. I'm leaving it in case someone wants an Objective-C answer and stumbles across this.
If I understood it correctly, to get expected date format you can use NSDateFormatter with following formatter:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat:#"EEEE, MMM d";
NSLog(#"%#", [dateFormatter stringFromDate:[NSDate date]]);
You can also set locale for date formatter to get weekdays in specified language:
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_GB"]];
For more details please refer to Data Formatting Guide
Updated Objective - c Code of #fennelouski :
- (NSString *)stringOfDatesOfThisWeek {
NSArray *datesOfThisWeek = [self datesOfTheWeek:[NSDate date]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE MMM d"];
NSMutableString *dateStringForThisWeek = [[NSMutableString alloc] initWithString:[formatter stringFromDate:[datesOfThisWeek firstObject]]];
for (int i = 1; i < datesOfThisWeek.count; i++) {
[dateStringForThisWeek appendFormat:#", %#", [formatter stringFromDate:[datesOfThisWeek objectAtIndex:i]]];
}
return dateStringForThisWeek;
}
- (NSArray *) datesOfTheWeek:(NSDate *)todaysDate {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger weekNumber = [[calendar components: NSCalendarUnitWeekOfMonth fromDate:todaysDate] weekOfMonth];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comp = [gregorian components:NSCalendarUnitYear fromDate:todaysDate];
[comp setWeekOfMonth:weekNumber]; //Week number.
NSMutableArray *dates = [NSMutableArray new];
for (int i = 0; i < 7; i++) {
[comp setWeekday:1]; //First day of the week. Change it to 7 to get the last date of the week
NSDate *resultDate = [gregorian dateFromComponents:comp];
[dates addObject:resultDate];
}
return dates;
}
Related
I have get Sequence of month using following code
NSDate *aDate = [NSDate dateWithTimeInterval:row*30*24*60*60 sinceDate:self.earliestPresentedDate];
NSDateComponents *components = [self.calendar components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:[NSDate date]];
NSDate *today = [self.calendar dateFromComponents:components];
components = [self.calendar components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:aDate];
NSDate *otherDate = [self.calendar dateFromComponents:components];
if ([today isEqualToDate:otherDate]) {
} else {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [NSLocale currentLocale];
formatter.dateFormat = #"MMM ";
[lblDate setText:[formatter stringFromDate:aDate]];
}
i want to get Sequence of year how to get...please give solution
Make one function which gives to date of next year as below,
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.year = +1;
NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDate *newDate = [theCalendar dateByAddingComponents:dayComponent toDate:[NSDate date] options:0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [NSLocale currentLocale];
formatter.dateFormat = #"yyyy";
NSString *strDate = [formatter stringFromDate:newDate];
NSLog(#"%#",strDate);
Set this date in your label.
above I am passing current date so I Will get year : 2016.
Simple way, it logs this and the subsequent 7 years.
NSInteger howManyYears = 8;
NSInteger thisYear = [[NSCalendar currentCalendar] component:NSCalendarUnitYear fromDate:[NSDate date]];
for (NSInteger i = 0; i < howManyYears; ++i) {
NSLog(#"%ld", thisYear++);
}
or to get an array of strings
NSMutableArray *theYears = [NSMutableArray array];
NSInteger howManyYears = 8;
NSInteger thisYear = [[NSCalendar currentCalendar] component:NSCalendarUnitYear fromDate:[NSDate date]];
for (NSInteger i = 0; i < howManyYears; ++i) {
[theYears addObject:[NSString stringWithFormat:#"%ld", thisYear++]];
}
NSLog(#"%#", theYears);
I am trying to get the beginning date of a month.
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"begining of month: %# from today", [self beginningOfMonth:[NSDate date]]);
}
- (NSDate *)beginningOfMonth:(NSDate *)date {
NSCalendar *calendar = [NSCalendar currentCalendar];
calendar.locale = [NSLocale currentLocale];
calendar.timeZone = [NSTimeZone defaultTimeZone];
calendar.firstWeekday = 2;
NSDateComponents *componentsCurrentDate = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitWeekOfMonth fromDate:date];
NSDateComponents *componentsNewDate = [NSDateComponents new];
componentsNewDate.year = componentsCurrentDate.year;
componentsNewDate.month = componentsCurrentDate.month;
componentsNewDate.weekOfMonth = 1;
componentsNewDate.weekday = calendar.firstWeekday;
return [calendar dateFromComponents:componentsNewDate];
}
But the console outputs is 2015-06-05 10:41:54.544 Test[1119:25066] begining of month: 2015-05-31 16:00:00 +0000
I just looked the calendar, it should be 2015-06-01 but it shows 2015-05-31. And I did set firstWeekday to 2, so it's Monday.
It seems you are getting the right result. Depending on the time zone of your Xcode installation, the console will output a different date.
Try using a date formatter in NSLog.
NSDateFormatter *f = [[NSDateFormatter alloc] init];
NSDate *firstMonday = [self beginningOfMonth:[NSDate date];
NSLog(#"First Monday of month: %#", [f stringFromDate:firstMonday]);
I need to get the weekday of the first day of the month. For example, for the current month September 2013 the first day falls on Sunday.
At first, get the first day of current month (for example):
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:(NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:today];
components.day = 1;
NSDate *firstDayOfMonth = [gregorian dateFromComponents:components];
Then use NSDateFormatter to print it as a weekday:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEEE"];
NSLog(#"%#", [dateFormatter stringFromDate:firstDayOfMonth]);
P.S. also take a look at Date Format Patterns
Here is the solution to getting the weekday name of the first day in the current month
NSDateComponents *weekdayComps = [[NSDateComponents alloc] init];
weekdayComps = [calendar.currentCalendar components:calendar.unitFlags fromDate:calendar.today];
weekdayComps.day = 1;
NSDateFormatter *weekDayFormatter = [[NSDateFormatter alloc]init];
[weekDayFormatter setDateFormat:#"EEEE"];
NSString *firstweekday = [weekDayFormatter stringFromDate:[calendar.currentCalendar dateFromComponents:weekdayComps]];
NSLog(#"FIRST WEEKDAY: %#", firstweekday);
For the weekday index, use this
NSDate *weekDate = [calendar.currentCalendar dateFromComponents:weekdayComps];
NSDateComponents *components = [calendar.currentCalendar components: NSWeekdayCalendarUnit fromDate: weekDate];
NSUInteger weekdayIndex = [components weekday];
NSLog(#"WEEKDAY INDEX %i", weekdayIndex);
You can also increment or decrement the month if needed.
Depending on the output you need you may use NSDateFormatter (as it was already said) or you may use NSDateComponents class. NSDateFormatter will give you a string representation, NSDateComponents will give you integer values. Method weekday may do what you want.
NSDateComponents *components = ...;
NSInteger val = [components weekday];
For Swift 4.2
First:
extension Calendar {
func startOfMonth(_ date: Date) -> Date {
return self.date(from: self.dateComponents([.year, .month], from: date))!
}
}
Second:
self.firstWeekDay = calendar.component(.weekday, from: calendar.startOfMonth(Date()))
Alright. The problem we're having is that we have NSStrings filled with dates
in the format of yyyyMMdd and what we want to do is to get the current weekday and name of month. The function dagOmvandlare converts the datestring to weekday and month. The function is then called on viewDidload to name all our button-titles from english to swedish months.
our current solution is looking like this:
-(NSString *)dagOmvandlare:(id) suprDatum{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = #"yyyyMMdd";
NSDate *date = [dateFormatter dateFromString:suprDatum];
NSString * monthString = [date descriptionWithCalendarFormat:#"%B"timeZone:nil
locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]];
if ([monthString isEqualToString:#"January"]) {
monthString = #"Januari";
}
else
if ([monthString isEqualToString:#"February"]) {
monthString = #"Februari";
}
else
if ([monthString isEqualToString:#"May"]) {
monthString = #"Maj";
}
else
if ([monthString isEqualToString:#"June"]) {
monthString = #"Juni";
}
else
if ([monthString isEqualToString:#"July"]) {
monthString = #"Juli";
}
else
if ([monthString isEqualToString:#"August"]) {
monthString = #"Augusti";
}
else
if ([monthString isEqualToString:#"October"]) {
monthString = #"Oktober";
}
else
if ([monthString isEqualToString:#"March"]) {
monthString = #"Mars";
}
return monthString;
}
- (void)viewDidLoad {
[super viewDidLoad];
[button3 setTitle:(NSString *)[self dagOmvandlare:self.datum1] forState:UIControlStateNormal];
[button2 setTitle:(NSString *)[self dagOmvandlare:self.datum2] forState:UIControlStateNormal];
[bmanad1 setTitle:(NSString *)[self dagOmvandlare:self.datum3] forState:UIControlStateNormal];
}
And what we've figured out so far is that the
NSString * monthString = [date descriptionWithCalendarFormat:#"%B"timeZone:nil
locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]];
is not allowed to be used by Apples guidelines regarding non-public api's. So the primary question is, what other way is there to get weekday and name of month out of our datestrings that contain dates with the format yyyyMMdd ?
There is no need to manually convert to the Swedish words. iPhone will do it for you. Try this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyyMMdd";
NSDate *date = [dateFormatter dateFromString:#"20111010"];
// set swedish locale
dateFormatter.locale=[[NSLocale alloc] initWithLocaleIdentifier:#"sv_SE"];
dateFormatter.dateFormat=#"MMMM";
NSString *monthString = [[dateFormatter stringFromDate:date] capitalizedString];
NSLog(#"month: %#", monthString);
dateFormatter.dateFormat=#"EEEE";
NSString *dayString = [[dateFormatter stringFromDate:date] capitalizedString];
NSLog(#"day: %#", dayString);
Output:
month: Oktober
day: Måndag
Simple Swift 3 extensions:
// Weekday
extension Date {
func dayOfWeek() -> String? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE"
return dateFormatter.string(from: self).capitalized
// or capitalized(with: locale)
}
}
print(Date().dayOfWeek()!) // Wednesday
// Month Name
extension Date {
func monthName() -> String? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM"
return dateFormatter.string(from: self).capitalized
// or capitalized(with: locale)
}
}
print(Date().monthName()!) // October
NSString *strDate=#"20110407";
NSDateFormatter *df=[[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:#"yyyyMMdd"];
NSDate *targetDate=[df dateFromString:strDate];
[df setDateFormat:#"EEEE MMMM dd, yyyy"];
NSString *s=[df stringFromDate:targetDate];
NSLog(#"Date: %#", s);
Hope this helps
NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];
NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
NSInteger day = [dateComponents day];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];
[calendar release];
This might be useful
Another SO question which might help you How to find weekday from today's date using NSDate?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEEE"];
NSString *dayName = [dateFormatter stringFromDate:Date];
this dayName will be available in the locale of user.
As apple says, creating (and let ARC dispose) a formatter is expensive, so:
1) declare in your file:
private var localDateFormatter : DateFormatter?
..
extension Date {
2) use lazy approach:
if localDateFormatter == nil{
localDateFormatter = DateFormatter()
let locale = Locale(identifier: "en_US_POSIX")
....}
let newTime = localDateFormatter!.date(from: self)
if You have alla the stuff in one class, a static / let member would be nice. (we cannot use vars in extensions... :( globals seems bad but no way...)
You can use (For Swift and Türkiye localization):
dateFormatter.locale = NSLocale(localeIdentifier: "tr_TR") as Locale
- (void) getMonthFromDate:(NSString *) stringDate
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormat dateFromString:stringDate];
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date]; // Get necessary date components
NSLog(#"%lu", [components day]);
NSLog(#"%lu %#",[components day],[[dateFormat monthSymbols] objectAtIndex:[components month]]);
}
How do I get the day of the week as a string?
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"EEEE"];
NSLog(#"%#", [dateFormatter stringFromDate:[NSDate date]]);
outputs current day of week as a string in locale dependent on current regional settings.
To get just a week day number you must use NSCalendar class:
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
int weekday = [comps weekday];
Just use these three lines:
CFAbsoluteTime at = CFAbsoluteTimeGetCurrent();
CFTimeZoneRef tz = CFTimeZoneCopySystem();
SInt32 WeekdayNumber = CFAbsoluteTimeGetDayOfWeek(at, tz);
Many of the answers here are deprecated. This works as of iOS 8.4 and gives you the day of the week as a string and as a number.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEEE"];
NSLog(#"The day of the week: %#", [dateFormatter stringFromDate:[NSDate date]]);
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps = [gregorian components:NSCalendarUnitWeekday fromDate:[NSDate date]];
int weekday = [comps weekday];
NSLog(#"The week day number: %d", weekday);
Here's how you do it in Swift 3, and get a localised day name…
let dayNumber = Calendar.current.component(.weekday, from: Date()) // 1 - 7
let dayName = DateFormatter().weekdaySymbols[dayNumber - 1]
-(void)getdate {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MMM dd, yyyy HH:mm"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"HH:mm:ss"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init] ;
[dateFormatter setDateFormat:#"EEEE"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];
NSString *theDate = [dateFormat stringFromDate:now];
NSString *theTime = [timeFormat stringFromDate:now];
NSString *week = [dateFormatter stringFromDate:now];
NSLog(#"\n"
"theDate: |%#| \n"
"theTime: |%#| \n"
"Now: |%#| \n"
"Week: |%#| \n"
, theDate, theTime,dateString,week);
}
I needed a simple (Gregorian) day of the week index, where 0=Sunday and 6=Saturday to be used in pattern match algorithms. From there it is a simple matter of looking up the day name from an array using the index. Here is what I came up with that doesn't require date formatters, or NSCalendar or date component manipulation:
+(long)dayOfWeek:(NSDate *)anyDate {
//calculate number of days since reference date jan 1, 01
NSTimeInterval utcoffset = [[NSTimeZone localTimeZone] secondsFromGMT];
NSTimeInterval interval = ([anyDate timeIntervalSinceReferenceDate]+utcoffset)/(60.0*60.0*24.0);
//mod 7 the number of days to identify day index
long dayix=((long)interval+8) % 7;
return dayix;
}
Here is the updated code for Swift 3
Code :
let calendar = Calendar(identifier: .gregorian)
let weekdayAsInteger = calendar.component(.weekday, from: Date())
To Print the name of the event as String:
let dateFromat = DateFormatter()
datFormat.dateFormat = "EEEE"
let name = datFormat.string(from: Date())
I think this topic is really useful, so I post some code Swift 2.1 compatible.
extension NSDate {
static func getBeautyToday() -> String {
let now = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEEE',' dd MMMM"
return dateFormatter.stringFromDate(now)
}
}
Anywhere you can call:
let today = NSDate.getBeautyToday()
print(today) ---> "Monday, 14 December"
Swift 3.0
As #delta2flat suggested, I update answer giving user the ability to specify custom format.
extension NSDate {
static func getBeautyToday(format: String = "EEEE',' dd MMMM") -> String {
let now = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
return dateFormatter.string(from: now)
}
}
Vladimir's answer worked well for me, but I thought that I would post the Unicode link for the date format strings.
http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns
This link is for iOS 6. The other versions of iOS have different standards which can be found in the X-Code documentation.
This way it works in Swift:
let calendar = NSCalendar.currentCalendar()
let weekday = calendar.component(.CalendarUnitWeekday, fromDate: NSDate())
Then assign the weekdays to the resulting numbers.
I had quite strange issue with getting a day of week. Only setting firstWeekday wasn't enough. It was also necesarry to set the time zone. My working solution was:
NSCalendar* cal = [NSCalendar currentCalendar];
[cal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[cal setFirstWeekday:1]; //Sunday
NSDateComponents* comp = [cal components:( NSWeekOfMonthCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSWeekCalendarUnit) fromDate:date];
return [comp weekday] ;
Swift 2: Get day of week in one line. (based on neoscribe answer)
let dayOfWeek = Int((myDate.timeIntervalSinceReferenceDate / (60.0*60.0*24.0)) % 7)
let isMonday = (dayOfWeek == 0)
let isSunday = (dayOfWeek == 6)
self.dateTimeFormatter = [[NSDateFormatter alloc] init];
self.dateTimeFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0]; // your timezone
self.dateTimeFormatter.locale = [NSLocale localeWithLocaleIdentifier:#"zh_CN"]; // your locale
self.dateTimeFormatter.dateFormat = #"ccc MM-dd mm:ss";
there are three symbols we can use to format day of week:
E
e
c
The following two documents may help you.
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns
Demo:
you can test your pattern on this website:
http://nsdateformatter.com/