Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Swift中设置日期样式_Swift - Fatal编程技术网

在Swift中设置日期样式

在Swift中设置日期样式,swift,Swift,我想在Swift中设置日期样式。 我已经在Objective-c中这样做了,这是Objective-c代码 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateStyle:(NSDateFormatterMediumStyle)]; NSDateFormatter *timeFormatter = [[NSDateFormatter alloc]init]; [timeF

我想在Swift中设置日期样式。 我已经在Objective-c中这样做了,这是Objective-c代码

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];

[dateFormatter setDateStyle:(NSDateFormatterMediumStyle)];

NSDateFormatter *timeFormatter = [[NSDateFormatter alloc]init];

[timeFormatter setDateFormat:@"h:mm a"];

NSString *fireDate = [dateFormatter stringFromDate:notification.fireDate];
NSString *fireTime = [timeFormatter stringFromDate:notification.fireDate];
NSString *dateTime = [NSString stringWithFormat:@"%@ - %@", fireDate, fireTime];
现在我要用斯威夫特。我已经想到了这个:

var dateFormatter = NSDateFormatter()
var timeFormatter = NSDateFormatter()

timeFormatter.dateFormat = "h:mm a"
//code to set date style.....
var fireDate: NSString = dateFormatter.stringFromDate(notification.fireDate)
var fireTime: NSString = timeFormatter.stringFromDate(notification.fireDate)
var dateTime: NSString = "\(fireDate) - \(fireTime)"

但我不知道如何在Swift中设置日期样式。使用NSDATEFORMATTERMENIUMSTYLE

dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
dateFormatter.dateStyle = .medium
你也可以用这个

 let formatter = NSDateFormatter()

 //format mm-dd-yy like 02-23-15
 formatter.dateStyle = .ShortStyle;


 //format yyyy-MM-dd like 2015-02-23
 formatter.dateFormat = "yyyy-MM-dd";


 //format feb 02, 2015 
 formatter.dateStyle = .MediumStyle;

在swift 3中,sintaxis有一点变化,没有NS前缀和小写枚举:

let myDate = Date()
let dateFormatter = DateFormatter()
//affects only the date representation of your date object
// xcode will let you know all the other available options
dateFormatter.dateStyle = .short
//the time part has it's own property (.short, .long, .none)
dateFormatter.timeStyle = .none

//01-27-17
print(dateFormatter.string(from: myDate))
请注意,默认情况下,格式化程序将使用设备区域的时区,对于某些应用程序,您可以通过格式化程序中的时区属性手动定义,也可以指定所需的格式

Swift 3:

dateFormatter.dateStyle = .medium
Swift 4:

DateFormatter() has 5 format style options for each of Date and Time. These are:
.none .short .medium .long .full

 DATE      TIME     DATE/TIME STRING
"none"    "none"    
"none"    "short"   9:42 AM
"none"    "medium"  9:42:27 AM
"none"    "long"    9:42:27 AM EDT
"none"    "full"    9:42:27 AM Eastern Daylight Time
"short"   "none"    10/10/17
"short"   "short"   10/10/17, 9:42 AM
"short"   "medium"  10/10/17, 9:42:27 AM
"short"   "long"    10/10/17, 9:42:27 AM EDT
"short"   "full"    10/10/17, 9:42:27 AM Eastern Daylight Time
"medium"  "none"    Oct 10, 2017
"medium"  "short"   Oct 10, 2017, 9:42 AM
"medium"  "medium"  Oct 10, 2017, 9:42:27 AM
"medium"  "long"    Oct 10, 2017, 9:42:27 AM EDT
"medium"  "full"    Oct 10, 2017, 9:42:27 AM Eastern Daylight Time
"long"    "none"    October 10, 2017
"long"    "short"   October 10, 2017 at 9:42 AM
"long"    "medium"  October 10, 2017 at 9:42:27 AM
"long"    "long"    October 10, 2017 at 9:42:27 AM EDT
"long"    "full"    October 10, 2017 at 9:42:27 AM Eastern Daylight Time
"full"    "none"    Tuesday, October 10, 2017
"full"    "short"   Tuesday, October 10, 2017 at 9:42 AM
"full"    "medium"  Tuesday, October 10, 2017 at 9:42:27 AM
"full"    "long"    Tuesday, October 10, 2017 at 9:42:27 AM EDT
"full"    "full"    Tuesday, October 10, 2017 at 9:42:27 AM Eastern Daylight Time
以下是Swift代码: 以下代码循环遍历其中的每一个,并显示生成的日期时间字符串:

import Foundation
let dateTimeFMT = DateFormatter()
var dateTimeString: String = ""
let currentDateTime = Date()
var stylesAsInts: [String] = [ // pre-formatted for display
    "\"none\"  ", "\"short\" ", "\"medium\"", "\"long\"  ", "\"full\"  "
] 
var dateStyleIdx: Int;    var timeStyleIdx: Int

print(" DATE      TIME     DATE/TIME STRING")
// LOOP through ALL time and date styles (.none .short .medium .long .full)
// these five values equate to 0...4
for dateStyleIdx     in 0...4 { // loop on DATE...
    dateTimeFMT.dateStyle     = DateFormatter.Style(rawValue: UInt(dateStyleIdx))!
    for timeStyleIdx in 0...4 { // loop on TIME...
        dateTimeFMT.timeStyle = DateFormatter.Style(rawValue: UInt(timeStyleIdx))!
        dateTimeString      = dateTimeFMT.string(from: currentDateTime)
        print("\(stylesAsInts[dateStyleIdx])  \(stylesAsInts[timeStyleIdx]) ", dateTimeString)
    }
}

不知道怎么做,但试着在
nsdateformatterminiumstyle
上使用方法
toRaw()
,非常感谢,这正是我想要的!请注意,@Dash的答案是完整答案。在这种情况下,已知dateStyle的类型为
NSDateFormatterStyle
,因此可以省略该部分。这是一个很好的示例。您还可以添加日期和时间格式都是本地化的。在我的例子中,日期是澳大利亚格式(日-月-年),我已将我的am/pm设置为小写。一切正常。