当前位置:网站首页>Date conversion EEE MMM DD hh:mm:ss zzz YYYY
Date conversion EEE MMM DD hh:mm:ss zzz YYYY
2022-07-29 01:09:00 【Hanyang Li】
1. Create date conversion classification
//
// Date+Extension.swift
//
import Foundation
extension Date{
/// Convert a formatted string to a date
static func fromData(string: String) ->Date?{
let df = DateFormatter()
df.locale = Locale(identifier: "en")
df.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
return df.date(from: string)
}
/**
Return the multi description information of the current date
just ( In a minute )
x Minutes ago ( In an hour )
x Hours before ( same day )
yesterday HH:mm( yesterday )
MM-dd HH:mm( Within a year )
yyyy-MM-dd HH:mm( Earlier )
*/
var dateDescription: String{
// Take out the current calendar - Provides a large number of calendar related operation functions
let calendar = Calendar.current
// Process today's date
if calendar.isDateInToday(self) {
let delta = Int(Date().timeIntervalSince(self))
if delta < 60 {
return " just "
}
if delta < 3600{
return "\(delta/60) Minutes ago "
}
return "\(delta/3600) Hours before "
}
// Date not today
var fmt = " HH:mm"
if calendar.isDateInYesterday(self){
fmt = " yesterday " + fmt
}else{
fmt = "MM-dd" + fmt
// Direct access to “ year ” The numerical
let comps = calendar.dateComponents([.year], from: self, to: Date())
if let year = comps.year, year > 0 {
fmt = "yyyy-" + fmt
}
}
// Generate a description string according to the format string
let df = DateFormatter()
df.dateFormat = fmt
df.locale = Locale(identifier: "en")
return df.string(from: self)
}
}2. The test method
override func viewDidLoad() {
super.viewDidLoad()
// The goal is : Special format string
// step
//1. establish ‘ date ’ object
//2. According to the date object , Calculate and display description characters
let str = "Mon Jul 25 16:41:43 +0800 2022"
print(Date.fromData(string: "Mon Jul 26 06:41:43 +0800 2022")?.dateDescription)
print(Date.fromData(string: "Mon Jul 26 10:03:01 +0800 2022")?.dateDescription)
print(Date.fromData(string: "Mon Jul 26 10:06:01 +0800 2022")?.dateDescription)
print(Date.fromData(string: "Mon Jul 26 00:00:02 +0800 2022")?.dateDescription)
print(Date.fromData(string: "Mon Jul 25 16:41:43 +0800 2022")?.dateDescription)
print(Date.fromData(string: "Mon Jul 24 16:41:43 +0800 2022")?.dateDescription)
print(Date.fromData(string: "Mon May 24 16:41:43 +0800 2022")?.dateDescription)
print(Date.fromData(string: "Mon May 28 16:41:43 +0800 2021")?.dateDescription)
}3. Output results
Optional("3 Hours before ")
Optional("3 Minutes ago ")
Optional(" just ")
Optional("10 Hours before ")
Optional(" yesterday 16:41")
Optional("07-24 16:41")
Optional("05-24 16:41")
Optional("2021-05-28 16:41")边栏推荐
- Inftnews | yuanuniverse shopping experience will become a powerful tool to attract consumers
- 【commons-lang3专题】004- NumberUtils 专题
- 【刷题笔记】链表内指定区间反转
- Instruction rearrangement, happens before, as if serial
- 教你一文解决 js 数字精度丢失问题
- ACM SIGIR 2022 | 美团技术团队精选论文解读
- 快手重点整治搬运、洗稿等方式的养号行为,自媒体平台如何净化内容生态
- [Commons lang3 topic] 002 randomutils topic
- 状态压缩dp-蒙德里安的梦想
- Main thread and daemon thread
猜你喜欢

【Jenkins笔记】入门,自由空间;持续集成企业微信;allure报告,持续集成电子邮件通知;构建定时任务

Wechat campus bathroom reservation for the finished product of applet graduation design (7) mid term inspection report

Day2: 130 questions in three languages

如何在WordPress中创建一个自定义404错误页面

Cookies and sessions

Method of converting inline elements to block elements

Some considerations about ThreadPool
![“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“](/img/f2/37a1e65eb1104d72128f96fc5d9c85.png)
“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“

Time series prediction | MATLAB realizes time series prediction of TCN time convolution neural network

Daniel guild Games: summary and future outlook of this year
随机推荐
🧐 Table1 | finish your third line watch in one second
Implement Lmax disruptor queue from scratch (VI) analysis of the principle of disruptor solving pseudo sharing and consumers' elegant stopping
教你一文解决 js 数字精度丢失问题
QT静态编译程序(Mingw编译)
PLATO上线LAAS协议Elephant Swap,用户可借此获得溢价收益
如何在WordPress中创建一个自定义404错误页面
Classification prediction | MATLAB realizes time series classification prediction of TCN time convolution neural network
“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“
[Commons lang3 topic] 004- numberutils topic
【刷题笔记】从链表中删去总和值为零的连续节点
如何给女友讲明白JS的bind模拟实现
AQS principle
B-tree~
转:认知亚文化
Main thread and daemon thread
Cookies and sessions
Cookie和Session
递归与分治
浅谈一下跨端技术方案
“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“