本文介绍了如何使用NSDateFormatter以对语言环境友好的方式打印一周中的某一天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到NSDateFormatter允许一个名为.FullStyle的枚举,该枚举将打印Tuesday, April 12, 1952 AD.

I noticed NSDateFormatter allows for an enum called .FullStyle that will print Tuesday, April 12, 1952 AD.

但是如何以区域安全的方式打印Tuesday?

But how do I just print Tuesday in a locale-safe way?

推荐答案

您需要使用NSDateFormatter dateFormat = "cccc".如果您需要有关日期格式模式的参考,可以查看以下链接:

You need to use NSDateFormatter dateFormat = "cccc". If you need a reference for date format patterns you can take a look at this link:

extension NSDate {
    struct Formatter {
        static let dayOfWeek: NSDateFormatter = {
            let formatter = NSDateFormatter()
            formatter.dateFormat = "cccc"   // Stand Alone local day of week
            return formatter
        }()
    }
    var dayOfWeek: String {
        return Formatter.dayOfWeek.stringFromDate(self)
    }
}

print(NSDate().dayOfWeek)   // Wednesday


Xcode 8 beta 3•Swift 3

extension DateFormatter {
    convenience init(dateFormat: String) {
        self.init()
        self.dateFormat = dateFormat
    }
}

extension Date {
    struct Formatter {
        static let dayOfWeek = DateFormatter(dateFormat: "cccc")  // Stand Alone local day of week
    }
    var dayOfWeek: String {
        return Formatter.dayOfWeek.string(from: self)
    }
}

print(Date().dayOfWeek)   // Wednesday

这篇关于如何使用NSDateFormatter以对语言环境友好的方式打印一周中的某一天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 09:29