-3

I am using this code to get the number of day in the current week:

static func currentDayOfWeek() -> Int {
    let comp = NSCalendar.current.dateComponents([.weekday], from: Date())
    if comp.weekday == 1 {
        return 7
    }

    return comp.weekday! - 1
}

Monday =  1
Tuesday = 2
...........
Sunday =  7

I can't manage to figure out how to get the current day number of the month and year.

For example for today current day number for the month should be 3 and for the year it should be 277.

Any suggestions ?

Wyetro
  • 8,439
  • 9
  • 46
  • 64
Adrian
  • 19,440
  • 34
  • 112
  • 219
  • 3
    Day of year: http://stackoverflow.com/questions/28712883/nsdate-day-of-the-year-swift – rmaddy Oct 03 '16 at 18:26
  • Day of month - just get the `day` component. – rmaddy Oct 03 '16 at 18:27
  • Remember, search is your friend. – rmaddy Oct 03 '16 at 18:28
  • Unrelated to your question but it seems your code is shifting the day of the week. Sunday -> Saturday. Monday -> Sunday, ..., Saturday -> Friday. Why are you doing this? – rmaddy Oct 03 '16 at 18:39
  • Because for me Sunday returns 1. I want 1 to be Monday. – Adrian Oct 03 '16 at 18:48
  • For everyone, Sunday returns 1. I get that from the code. My question is why do you want Monday to be 1 instead of the standard 2? All of the APIs treat Sunday as 1, Monday as 2 etc. – rmaddy Oct 03 '16 at 18:52
  • Because I need to know how many days passed from the current week to calculate some averages – Adrian Oct 04 '16 at 12:24

3 Answers3

7
let dayYear = Calendar.current.ordinality(of: .day, in: .year, for: Date())

let dayMonth = Calendar.current.ordinality(of: .day, in: .month, for: Date())
rmaddy
  • 314,917
  • 42
  • 532
  • 579
LDNZh
  • 1,136
  • 8
  • 14
1
func currentDayOfMonth()->Int {
    return NSCalendar.currentCalendar().ordinalityOfUnit(NSCalendarUnit.Day, inUnit: NSCalendarUnit.Month, forDate: NSDate())
}

func currentDayOfYear()->Int {
    return NSCalendar.currentCalendar().ordinalityOfUnit(NSCalendarUnit.Day, inUnit: NSCalendarUnit.Year, forDate: NSDate())
}
Serge K
  • 96
  • 1
  • 3
-1

The original answer is from this post.

This will return "Monday" or the day of the week:

func getDayOfWeek(_ today:String) -> String? {

    let weekdays = [1:"Sunday", 2:"Monday", 3:"Tuesday", 4:"Wednesday", 5:"Thursday", 6:"Friday", 7:"Saturday"]

    let formatter  = DateFormatter()
    formatter.dateFormat = "MM-dd-yyyy"
    if let todayDate = formatter.date(from: today) {
        let myCalendar = Calendar(identifier: .gregorian)
        let weekDay = myCalendar.component(.weekday, from: todayDate)
        let day = weekdays[weekDay]

        return day
    } else {
        return nil
    }
}

getDayOfWeek("10-03-2016")

This will return "1" or the day's number for the week:

func getDayOfWeek(_ today:String) -> Int? {

    let formatter  = DateFormatter()
    formatter.dateFormat = "MM-dd-yyyy"
    if let todayDate = formatter.date(from: today) {
        let myCalendar = Calendar(identifier: .gregorian)
        let weekDay = myCalendar.component(.weekday, from: todayDate)

        return weekDay
    } else {
        return nil
    }
}

getDayOfWeek("10-03-2016")
Community
  • 1
  • 1
Jay
  • 43
  • 4
  • 1) The question didn't ask how to get the weekday. 2) The question didn't ask about parsing strings. 3) The first method has a hardcode array of weekday names in English. People using other languages won't like that. `DateFormatter` already has a property with weekday names in the appropriate language for a given locale. – rmaddy Oct 04 '16 at 14:49