简体   繁体   English

如何在Swift中更改星期的第一天

[英]How to change the first day of week in Swift

I have made a functioning app and part of it includes formatting the date from a date picker. 我制作了一个功能良好的应用程序,其中一部分包括通过日期选择器格式化日期。

I need to change the first day of the week as the week days are being displayed as "1" - "7". 我需要更改一周的第一天,因为工作日显示为“ 1”-“ 7”。 However, day 1 is currently Sunday and I need day 1 to be Monday and Sunday as day 7. 但是,第1天当前是星期日,我需要将第1天设为星期一和星期日作为第7天。

The code for my date formatter and picker are below: 我的日期格式化程序和选择器的代码如下:

var chosenDate = self.datePicker.date
var formatter = NSDateFormatter()        
formatter.dateFormat = "ewYY"

let day = formatter.stringFromDate(chosenDate)
let dateResult = "\(day)"

DestViewController.date = dateResult

I got all of my date formatting info from this page: 我从此页面获得了所有日期格式信息:

http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns

I just can't seem to work out how to change this first day of the week? 我只是似乎无法解决如何在一周的第一天进行更改?

Many thanks in advance Mark. 非常感谢马克。

Here is good example in how to manipulate date in swift. 这是一个如何快速处理日期的好例子。 you can change the code to fit it better for what you may need, but right now it does what you need. 您可以更改代码以使其更适合您的需求,但是现在它可以满足您的需求。

// Playground - noun: a place where people can play

// Setup the calendar object
let calendar = NSCalendar.currentCalendar()

// Set up date object
let date = NSDate()
// Create an NSDate for the first and last day of the month
let components = NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitMonth, fromDate: date)


components.month

// Getting the First and Last date of the month
components.day = 1
let firstDateOfMonth: NSDate = calendar.dateFromComponents(components)!

components.month  += 1
components.day     = 0
let lastDateOfMonth: NSDate = calendar.dateFromComponents(components)!

var unitFlags = NSCalendarUnit.WeekOfMonthCalendarUnit |
    NSCalendarUnit.WeekdayCalendarUnit     |
    NSCalendarUnit.CalendarUnitDay

let firstDateComponents = calendar.components(unitFlags, fromDate: firstDateOfMonth)
let lastDateComponents  = calendar.components(unitFlags, fromDate: lastDateOfMonth)

// Sun = 1, Sat = 7
let firstWeek = firstDateComponents.weekOfMonth
let lastWeek  = lastDateComponents.weekOfMonth

let numOfDatesToPrepend = firstDateComponents.weekday - 1
let numOfDatesToAppend  = 7 - lastDateComponents.weekday + (6 - lastDateComponents.weekOfMonth) * 7

let startDate: NSDate = calendar.dateByAddingUnit(NSCalendarUnit.CalendarUnitDay, value: -numOfDatesToPrepend, toDate: firstDateOfMonth, options: nil)!
let endDate:   NSDate = calendar.dateByAddingUnit(NSCalendarUnit.CalendarUnitDay, value: numOfDatesToAppend, toDate: lastDateOfMonth, options: nil)!

Array(map(0..<42) {
    calendar.dateByAddingUnit(NSCalendarUnit.CalendarUnitDay, value: $0, toDate: startDate, options: nil)!
    })

"\(components.year)"


//var dateString = stringFromDate(NSDate())// change to your date format
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EE"
var dateString = dateFormatter.stringFromDate(NSDate())
var xdate = dateFormatter.dateFromString(dateString)
//var someDate = dateFormatter.dateString
println(dateString)

this will output:: 这将输出:

"Thu"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM