如何从Swift中的日期中提取年数,月数和天数。
到目前为止,我在名为DatePickerController
的类文件中有以下代码。
class DatePickerController: UIViewController {
@IBOutlet var datePicker:UIDatePicker!
@IBOutlet var dateDisplay: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func datePickerChanged(sender: AnyObject) {
setDate()
}
let dateFormatter = NSDateFormatter()
// MARK: - Date Format
func setDate() {
dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
dateDisplay.text = dateFormatter.stringFromDate(datePicker.date)
}
}
我将为年,月,日创建单独的UILables
。
答案 0 :(得分:2)
您可以使用// change this
$a = $xd1->ServiceHotel[0]->BookingStatus;
$b = $xd1->ServiceHotel[1]->BookingStatus;
// to this
$a = $xd1->ServiceHotel[0]->BookingStatus->asXML();
$b = $xd1->ServiceHotel[1]->BookingStatus->asXML();
在两个日期之间获取NSCalendar
。在Swift 3中:
NSDateComponents
或者在Swift 2中:
let components = Calendar.current.dateComponents([.day, .month, .year], from: date1, to: date2)
let dayString = "\(components.day!)"
let monthString = "\(components.month!)"
let yearString = "\(components.year!)"
也许更简单,如果您想要一个代表这两个日期之间经过时间的字符串,您可以使用let components = NSCalendar.currentCalendar().components([.Day, .Month, .Year], fromDate: date1, toDate: date2, options: [])
let dayString = "\(components.day)"
let monthString = "\(components.month)"
let yearString = "\(components.year)"
,例如,在Swift 3中:
NSDateComponentsFormatter
或者在Swift 2中:
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.day, .month, .year]
formatter.unitsStyle = .full
let string = formatter.string(from: date1, to: date2)
此字符串也已本地化。
答案 1 :(得分:2)
就像你说的那样,每个日期组件都需要UILabel。然后,您可以使用自定义日期格式字符串提取组件,而不是使用NSDateFormatterStye.ShortStyle
。这为您提供了最大的灵活性。格式字符串遵循unicode standard。
我无法在项目的上下文中测试代码,但如果添加标签并将它们正确连接到Xcode,我相信这段代码将完全符合您的要求:
class DatePickerController: UIViewController {
@IBOutlet var datePicker:UIDatePicker!
@IBOutlet var dayDisplay: UILabel!
@IBOutlet var monthDisplay: UILabel!
@IBOutlet var yearDisplay: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func datePickerChanged(sender: AnyObject) {
setDate()
}
let dateFormatter = NSDateFormatter()
// MARK: - Date Format
func setDate() {
dateFormatter.dateFormat = "dd"
dayDisplay.text = dateFormatter.stringFromDate(datePicker.date)
dateFormatter.dateFormat = "MM"
monthDisplay.text = dateFormatter.stringFromDate(datePicker.date)
dateFormatter.dateFormat = "yyyy"
yearDisplay.text = dateFormatter.stringFromDate(datePicker.date)
}
}
答案 2 :(得分:0)
您可以使用components(fromDate: toDate: options:)
<{1}}方法NSCalender
来使用let date = NSDate(timeIntervalSinceNow: -123123123)
extension NSDate {
func intereval(date:NSDate) -> (year: Int, month:Int, day:Int) {
let calender = NSCalendar.currentCalendar()
let components = calender.components([.Year, .Month, .Day], fromDate: date, toDate: self, options:.MatchStrictly)
return (components.year, components.month, components.day)
}
}
let component = NSDate().intereval(date)
print(component.year)
print(component.month)
print(component.day)
来检索年,月和日
示例
{{1}}
答案 3 :(得分:0)
import Foundation
let now = NSDate()
now.description.characters.split {
" -".characters.contains($0)
}.enumerate().forEach {
print($0.0,String($0.1))
}
/*
0 2015
1 12
2 26
3 19:34:33
4 +0000
*/