let date = Date()
var date: [Int] = [] let calendar: Calendar = Calendar(identifier: .gregorian) var comps: DateComponents = DateComponents() comps = calendar.dateComponents([.year, .month, .day], from: Date()) date.append(comps.year!) date.append(comps.month!) date.append(comps.day!)
func getCountOfDaysInMonth(year: Int, month: Int) -> (count: Int, week: Int) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM"
let date = dateFormatter.date(from: String(year)+"-"+String(month))
let calendar: Calendar = Calendar(identifier: .gregorian)
let range = calendar.range(of: .day, in: .month, for: date!)
let week = calendar.component(.weekday, from: date!)
return ((range?.count)!, week)
}
EEEE: 代表一天的全名,比如Monday.使用1-3个E就代表简写,比如Mon. MMMM: 代表一个月的全名,比如July.使用1-3个M就代表简写,比如Jul. dd: 代表一个月里的几号,比如07或者30. yyyy: 代表4个数字表示的年份,比如2016. HH: 代表2个数字表示的小时,比如08或17. mm: 代表2个数字表示的分钟,比如01或59. ss: 代表2个数字表示的秒,比如2016. zzz: 代表3个字母表示的时区,比如GTM(格林尼治标准时间,GMT+8为北京所在的时区,俗称东八区) GGG: BC或者AD, 即公元前或者公元
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
// 返回Section的数量
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 0
}
// 返回Item的数量
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 0
}
// 返回Cell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "dateItem", for: indexPath) as! dateCollectionViewCell
return cell
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// 这两句话很重要!!!
CalendarCollectionView.dataSource = self
CalendarCollectionView.delegate = self
}
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
// 返回Section的数量
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
// 返回Item的数量
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
return weekArray.count
} else {
return 42
}
}
// 返回Cell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "dateItem", for: indexPath) as! dateCollectionViewCell
if indexPath.section == 0 {
cell.textLabel.text = weekArray[indexPath.row]
} else {
var daysArray: [String] = []
// 第一天之前的空白区域
for number in 0..<firstDayOfMonth-1 {
daysArray.append("")
}
for number in firstDayOfMonth-1...firstDayOfMonth+numberOfTheMonth-2 {
daysArray.append(String(number-firstDayOfMonth+2))
}
// 最后一天后的空白区域
for number in firstDayOfMonth+numberOfTheMonth-2...41 {
daysArray.append("")
}
cell.textLabel.text = daysArray[indexPath.row]
}
return cell
}
}
@IBAction func lastMonth(_ sender: UIButton) {
if month == 1 {
year -= 1
month = 12
}else {
month -= 1
}
dateDisplayLabel.text = String(year)+"-"+String(month)
firstDayOfMonth = date.getCountOfDaysInMonth(year: year, month: month).week
numberOfTheMonth = date.getCountOfDaysInMonth(year: year, month: month).count
CalendarCollectionView.reloadData()
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有