iOS: How to get a proper Month name from a number?
Another option is to use the monthSymbols method:
int monthNumber = 11; //November
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
NSString *monthName = [[df monthSymbols] objectAtIndex:(monthNumber-1)];
Note that you'll need to subtract 1 from your 1..12 monthNumber since monthSymbols is zero-based.
Get month number from month name?
Quick answer:
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MMM"];
NSDate *aDate = [formatter dateFromString:@"Jul"];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSMonthCalendarUnit fromDate:aDate];
NSLog(@"Month: %i", [components month]); /* => 7 */
See date formatters.
iphone how can i get month name from month number?
Something like this:
int monthNumber = 11;
NSString * dateString = [NSString stringWithFormat: @"%d", monthNumber];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM"];
NSDate* myDate = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMMM"];
NSString *stringFromDate = [formatter stringFromDate:myDate];
[formatter release];
NSLog(@"%@", stringFromDate);
Get next 3rd month name from the current month in swift
You need to use the Calendar class, e.g.
var now = Date()
var calendar = Calendar.current
if let then = calendar.date(byAdding: .month, value: 3, to: now) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "LLLL"
let monthName = dateFormatter.string(from: then)
print ("\(monthName)")
}
Just keep in mind how calenar arithmetics is handled: if you add "3 months" to let's say Nov 30th, 2019, then you'll get Feb-29th, 2020, although someone might expect March-01, 2020.
How to get month name with year and list of years between two Date
Use below code to get a list of years between two dates.
func getYear(){
let startDateString:String = "05/21/2014"
let endDateString:String = "05/21/2017"
let dateFormtter = DateFormatter()
dateFormtter.dateFormat = "MM/dd/yyyy"
let startDate = dateFormtter.date(from: startDateString)
let endDate = dateFormtter.date(from: endDateString)
var arrYears = [String]()
dateFormtter.dateFormat = "MM"
if let startYear: Int = startDate?.year(), let endYear = endDate?.year() {
for i in startYear...endYear {
let monthWithYear = "\(i)"
arrYears.append(monthWithYear)
}
}
}
extension Date {
func Year() -> Int {
let year = Calendar.current.component(.year, from: self)
return year
}
}
NSDateFormatter not showing full month name, only single digit number
Turns out to be an issue with the second line, setLocale
. I assume that the system won't default to using english month names when the locale
has been manually set? I live in an english speaking locale
, but maybe it doesn't matter.
In any case, not setting the locale fixes the month name issue.
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
[dateFormat setDateFormat:@"MMMM dd, h:mm a"];
Convert long month name to int
Use MM
for the month format. Use stringFromDate
to convert your NSDate
to a String
. Then convert your string to an Int
with .toInt()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM"
let monthString = dateFormatter.stringFromDate(NSDate()) // "05"
let monthInt = monthString.toInt() // 5
Properly ordering month and date according to locale
Consider DateIntervalFormatter
:
let formatter = DateIntervalFormatter()
formatter.locale = locale
formatter.dateTemplate = "MMMMd"
let string = formatter.string(from: date1, to: date2)
For the same month, that will generate “7 – 13 de marzo” and “March 7 – 13”.
For different months, “7 de febrero – 13 de marzo” and “February 7 – March 13”.
There are weird edge cases with these formatters (e.g., a dateStyle
of .long
works in English speaking locales, but not in others). And I wouldn’t be surprised if the above was not working perfectly in all of your target locales, so you might want to double check. But, in my experience, these formatters can get you pretty close. It appears to achieve what you need in English and Spanish…
How to convert Array of Months Name to Months Number in swift
A function like this may help
func getMonthIndex(_ month: String) -> Int {
let months = ["JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"]
var monthIndex = -1
if let idx = months.index(of: month.uppercased()) {
monthIndex = idx + 1
}
return monthIndex
}
which can be used as follows :
var idx = getMonthIndex("January") //1
idx = getMonthIndex("JANUARY") //1
idx = getMonthIndex("DECEMBER") //12
idx = getMonthIndex("DECEMBERRRR") //-1
In the snipped above "month.uppercased()" is very important this will help to identify months in all cases such as "January", "JANUARY" OR "january"
Related Topics
Force View Controller to Reload to Refresh Uiappearance Changes
Create Tap-Able "Links" in the Nsattributedstring of a Uilabel
How to Link to Apps on the App Store
Undefined Symbols For Architecture Arm64
Assertion Failure in Dequeuereusablecellwithidentifier:Forindexpath:
How to Navigate Through Textfields (Next/Done Buttons)
Uilabel Sizetofit Doesn't Work With Autolayout Ios6
How to Save Picture to Iphone Photo Library
Check If There Is an Emoji Contained in a String
What Happens When Testflight App Expire
How to Add Constraints Programmatically Using Swift
Nsurlsession/Nsurlconnection Http Load Failed on iOS 9
When Should I Compare an Optional Value to Nil
How Would I Create a Uialertview in Swift