我有一个NSDate对象,我想将它设置为任意时间(比如半夜),这样我可以使用timeIntervalSince1970的功能一致,而不用担心时间来检索数据时创建的对象.
我尝试使用NSCalendar一些Objective-C方法来修改它的组件,如下所示:
let date: NSDate = NSDate()
let cal: NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)!
let components: NSDateComponents = cal.components(NSCalendarUnit./* a unit of time */CalendarUnit, fromDate: date)
let newDate: NSDate = cal.dateFromComponents(components)
Run Code Online (Sandbox Code Playgroud)
上述方法的问题在于您只能设置一个时间单位(/* a unit of time */),因此您只能使用下列其中一个准确度:
有没有办法同时设置小时,分钟和秒并保留日期(日/月/年)?
LocalDate#toDateMidnight读取的javdoc 如下:
从v1.5开始,建议您避免使用DateMidnight并使用toDateTimeAtStartOfDay(),因为下面有详细说明.
如果默认时区在午夜切换到夏令时,则此方法将引发异常,此LocalDate表示该切换日期.问题是在所需的日期没有午夜这样的时间,因此抛出了这样的例外.
在某些时区中不存在午夜这一事实似乎足以避免DateMidnight完全使用(假设您的代码没有使用已知不具有此DST情况的固定时区,并且永远不需要在其中使用不同的时区未来).
但是,DateMidnight不会弃用,并且DateMidnight类本身的javadoc中没有类似的建议或警告.此外,DateMidnight构造函数愉快地接受即时和时区,使得在给定的一天不存在午夜,而不是投掷IllegalArgumentException类似的LocalDate#toDateMidnight.结果DateMidnight表现为DateTime在一天开始时的时间.
当某个特定日期不存在午夜时,为什么LocalDate#toDateMidnight在DateMidnight构造函数没有的情况下抛出异常?推荐的用例是DateMidnight什么?
我很难将库图表(来自Daniel Gindi)从版本2(Swift 2.3)迁移到3(Swift 3).
基本上,我不能让x标签(日期)与相应的图正确对齐.
这是我之前在第2版中所拥有的:
在版本2中,我有第7天,第8天,第10天和第11天的值.所以我错过了中间的一天,但标签正确地与图表一起使用.

这是我在版本3中的内容:
在版本3中,x轴中的"标签"现在已被替换为double(对于日期,它是自1970年以来的timeInterval),并通过格式化程序进行格式化.因此,无可否认,图表现在更"正确",因为图表正确地推断出第9个值,但我找不到如何将标签放在相应的图表下面.

这是我的x轴代码:
let chartView = LineChartView()
...
let xAxis = chartView.xAxis
xAxis.labelPosition = .bottom
xAxis.labelCount = entries.count
xAxis.drawLabelsEnabled = true
xAxis.drawLimitLinesBehindDataEnabled = true
xAxis.avoidFirstLastClippingEnabled = true
// Set the x values date formatter
let xValuesNumberFormatter = ChartXAxisFormatter()
xValuesNumberFormatter.dateFormatter = dayNumberAndShortNameFormatter // e.g. "wed 26"
xAxis.valueFormatter = xValuesNumberFormatter
Run Code Online (Sandbox Code Playgroud)
这是我创建的ChartXAxisFormatter类:
import Foundation
import Charts
class ChartXAxisFormatter: NSObject {
var dateFormatter: DateFormatter?
}
extension ChartXAxisFormatter: IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String …Run Code Online (Sandbox Code Playgroud)