0

I am trying to display the week number of current date, and the date range of the week number. Right now the date range format is DateTime, I would like to change it to "Jan 13"instead. Any recommendations on how I can fix this?

override func viewDidLoad() {
    super.viewDidLoad()


    let dateToday = Date()
    let weekNumber = NSCalendar.current.component(.weekOfYear, from: dateToday)
    let weekRange = dayRangeOf(weekOfYear: weekNumber, for: dateToday)

    weekLabel.text = String("Week #\(weekNumber): \(weekRange)")
}

func dayRangeOf(weekOfYear: Int, for date: Date) -> Range<Date>
{
    let calendar = Calendar.current
    let year = calendar.component(.yearForWeekOfYear, from: date)
    let startComponents = DateComponents(weekOfYear: weekOfYear, yearForWeekOfYear: year)
    let startDate = calendar.date(from: startComponents)!
    let endComponents = DateComponents(day:7, second: -1)
    let endDate = calendar.date(byAdding: endComponents, to: startDate)!
    return startDate..<endDate
}
Jenna
  • 11
  • 2
  • Your question is very unclear. Please [edit] your question and clearly explain what input you are providing, the output your are currently getting, and the exact output you actually want. `"mm, d"` is not a date range. A string such as `Jan 13` is not in the format `mm, d`. `MMM d` would give `Jan 13`. – rmaddy Jan 13 '19 at 20:03

2 Answers2

1

There is a dedicated API to display date intervals: DateIntervalFormatter

let intervalForatter = DateIntervalFormatter()
intervalFormatter.timeStyle = .none
intervalFormatter.dateStyle = .medium
weekLabel.text = String("Week #\(weekNumber): \(intervalFormatter.string(from: weekRange.lowerBound, to: weekRange.upperBound))")

You can also use a custom date format (by the way Jan 13 is MMM d)

let intervalFormatter = DateIntervalFormatter()
intervalFormatter.dateTemplate = "MMM d"
let text = String("Week #\(weekNumber): \(intervalFormatter.string(from: weekRange.lowerBound, to: weekRange.upperBound))")
vadian
  • 274,689
  • 30
  • 353
  • 361
  • I used the custom date format, this totally worked. Exactly what I was looking for. Thank you so much! – Jenna Jan 14 '19 at 01:45
0

You can user answer from: Date Format in Swift

let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd HH:mm:ss"

let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "MM d"

if let date = dateFormatterGet.date(from: "2016-02-29 12:24:26") {
    print(dateFormatterPrint.string(from: date))
} else {
   print("There was an error decoding the string")
}
George Heints
  • 1,303
  • 3
  • 20
  • 37
  • 2
    The OP says they want 3 letter month names. That would be a format of "MMM d", wouldn't it?\ – Duncan C Jan 13 '19 at 18:49
  • You can use any format, use https://nsdateformatter.com to get the format you want. – George Heints Jan 13 '19 at 18:50
  • 1
    My point is that the OP was asking for a specific output format, and I don't think your answer provides that format. I don't use date formats often enough to have them memorized, but i seem to remember that 3 letter month names use the `MMM` format code. – Duncan C Jan 13 '19 at 19:18
  • @DuncanC like i said before you can use nsdateformatter.com to get right format. It provide all variations of date to you. If you need Jan, 01... use "MMM, dd" – George Heints Jan 13 '19 at 19:21