我想以这种格式显示日历
给用户。一种选择是使用"字符串范围"获取各个日历组件。第二个是使用NSCalendar来获取它对我来说看起来更好(是吗?)。所以我的代码如下。但是有两个问题。
任何人都知道如何得到我需要的东西?附加图像是我想要实现的目标。在那里,我使用三个UILabel用于" date",第二个用于"月,年" "时间"
任何帮助都将不胜感激。
var inputDateString = "Jun/12/2015 02:05 Am +05:00"
override func viewDidLoad() {
super.viewDidLoad()
let newDate = dateformatterDateString(inputDateString)
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitMonth | .CalendarUnitYear | .CalendarUnitDay, fromDate: newDate!)
let hour = components.hour
let minutes = components.minute
let month = components.month
let year = components.year
let day = components.day
println(newDate)
println(components)
println(day) // 12
println(month) // 6 -----> Want to have "Jun" here
println(year) // 2015
println(hour) // 2 ------> Want to have the hour in the inputString i.e. 02
println(minutes) // 35 ------> Want to have the minute in the inputString i.e. 05
}
func dateformatterDateString(dateString: String) -> NSDate? {
let dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMM/dd/yyyy hh:mm a Z"
// dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
dateFormatter.timeZone = NSTimeZone.localTimeZone()
return dateFormatter.dateFromString(dateString)
}
答案 0 :(得分:23)
您可以使用DateFormatter,如下所示:
extension Formatter {
static let monthMedium: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "LLL"
return formatter
}()
static let hour12: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "h"
return formatter
}()
static let minute0x: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "mm"
return formatter
}()
static let amPM: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "a"
return formatter
}()
}
extension Date {
var monthMedium: String { return Formatter.monthMedium.string(from: self) }
var hour12: String { return Formatter.hour12.string(from: self) }
var minute0x: String { return Formatter.minute0x.string(from: self) }
var amPM: String { return Formatter.amPM.string(from: self) }
}
let date = Date()
let dateMonth = date.monthMedium // "May"
let dateHour = date.hour12 // "1"
let dateMinute = date.minute0x // "18"
let dateAmPm = date.amPM // "PM"
答案 1 :(得分:17)
NSDateFormatter
有monthSymbols
,shortMonthSymbols
和veryShortSymbols
属性。
所以试试这个:
let dateFormatter: NSDateFormatter = NSDateFormatter()
let months = dateFormatter.shortMonthSymbols
let monthSymbol = months[month-1] as! String // month - from your date components
println(monthSymbol)
答案 2 :(得分:2)
I am adding three types. Have a look.
//Todays Date
let todayDate = NSDate()
let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)!
let components = calendar.components(.CalendarUnitYear | .CalendarUnitMonth | .CalendarUnitDay, fromDate: todayDate)
var (year, month, date) = (components.year, components.month, components.day)
println("YEAR: \(year) MONTH: \(month) DATE: \(date)")
//Making a X mas Yr
let morningOfChristmasComponents = NSDateComponents()
morningOfChristmasComponents.year = 2014
morningOfChristmasComponents.month = 12
morningOfChristmasComponents.day = 25
morningOfChristmasComponents.hour = 7
morningOfChristmasComponents.minute = 0
morningOfChristmasComponents.second = 0
let morningOfChristmas = NSCalendar.currentCalendar().dateFromComponents(morningOfChristmasComponents)!
let formatter = NSDateFormatter()
formatter.dateStyle = NSDateFormatterStyle.LongStyle
formatter.timeStyle = .MediumStyle
let dateString = formatter.stringFromDate(morningOfChristmas)
print("dateString : \(dateString)")
//Current month - complete name
let dateFormatter: NSDateFormatter = NSDateFormatter()
let months = dateFormatter.monthSymbols
let monthSymbol = months[month-1] as! String
println("monthSymbol : \(monthSymbol)")
Print Results:
YEAR: 2015 MONTH: 10 DATE: 9
dateString : December 25, 2014 at 7:00:00 AM
monthSymbol : October
答案 3 :(得分:0)
Swift 4.x解决方案:
//if currentMonth = 1
DateFormatter().monthSymbols[currentMonth - 1]
答案:
一月
答案 4 :(得分:0)
更新Swift 5.x解决方案:
今天是public class App extends Application {
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
private XYChart.Series<Number, Number> seriesTotal = new XYChart.Series<Number, Number>();
private LineChart<Number, Number> chart = new LineChart<>(xAxis, yAxis);
@Override
public void start(Stage stage) throws Exception {
BorderPane b = new BorderPane();
MyService ser = new MyService();
// Job j = new Job();
Button button = new Button("start");
Button button1 = new Button("start2");
List<Object> test = new ArrayList<>();
button.setOnAction(((event) -> {
test.clear();
ser.addEventHandler(WorkerStateEvent.WORKER_STATE_SUCCEEDED, //
new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent t) {
seriesTotal = ser.getValue();
//test.add(ser.getValue());
//chart.getData().add(ser.getValue());
}
});
ser.restart();
System.out.println(seriesTotal);
chart.getData().add(seriesTotal);
seriesTotal.getData().clear();
}));
button1.setOnAction(((event) -> {
XYChart.Series<Number, Number> series1 = new XYChart.Series<Number, Number>();
series1.getData().add(new XYChart.Data<Number, Number>(Math.random(), Math.random()));
series1.getData().add(new XYChart.Data<Number, Number>(Math.random(), Math.random()));
series1.getData().add(new XYChart.Data<Number, Number>(Math.random(), Math.random()));
chart.getData().add(series1);
// chart.getData().setAll(arg0);
}));
b.setBottom(button);
b.setCenter(chart);
b.setLeft(button1);
Scene sc = new Scene(b);
stage.setScene(sc);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
enter code here
Monday, 20 April, 2020
现在,您可以按以下方式获取年,月,周,日的所有索引值,如下所示:
let date = Date() // get a current date instance
let dateFormatter = DateFormatter() // get a date formatter instance
let calendar = dateFormatter.calendar // get a calendar instance
您可以获得所有月份名称的数组,例如:
let year = calendar?.component(.year, from: date) // Result: 2020
let month = calendar?.component(.month, from: date) // Result: 4
let week = calendar?.component(.weekOfMonth, from: date) // Result: 4
let day = calendar?.component(.day, from: date) // Result: 20
let weekday = calendar?.component(.weekday, from: date) // Result: 2
let weekdayOrdinal = calendar?.component(.weekdayOrdinal, from: date) // Result: 3
let weekOfYear = calendar?.component(.weekOfYear, from: date) // Result: 17
您可以根据需要设置当前日期的格式:
let monthsWithFullName = dateFormatter.monthSymbols // Result: ["January”, "February”, "March”, "April”, "May”, "June”, "July”, "August”, "September”, "October”, "November”, "December”]
let monthsWithShortName = dateFormatter.shortMonthSymbols // Result: ["Jan”, "Feb”, "Mar”, "Apr”, "May”, "Jun”, "Jul”, "Aug”, "Sep”, "Oct”, "Nov”, "Dec”]
我认为这是最简单和最新的答案。