将字符串转换为返回前一天的日期 [英] Converting string to date returning the day before

查看:65
本文介绍了将字符串转换为返回前一天的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个格式为2017-03-14"(yyyy-MM-dd)的字符串中的日期,我正在尝试将其转换为格式为2017 年 3 月 14 日,星期二"的字符串.

I have a date in a string with this format "2017-03-14" (yyyy-MM-dd) and i am trying to convert it into a string with this format "Tuesday, March 14, 2017".

这是我的代码:

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: "2017-03-14")

现在,如果我打印日期"的值,我会得到:

Now if i print the value of "date", i get this:

2017-03-13 22:00:00 +0000

这只是前一天.这是为什么?

This is just the day before. Why is that ?

我需要在格式化之前比较日期.

I need to compare date before formatting it.

var newDateString : String = ""

let date2 = Date()

let comp = date2.compare(date!)

if comp.rawValue == 0 {
    newDateString = "TONIGHT"
} else {
    dateFormatter.dateFormat = "EEEE, MMMM d, yyyy"
    newDateString = dateFormatter.string(from: date!)
}

谢谢

推荐答案

您的困惑是基于对时间和日期的误解.显然,您当前所在的时区比 UTC(协调世界时)早 2 小时,以前称为格林威治标准时间 (GMT).

Your confusion is based on a misunderstanding of what Time and Date are. Evidently, you are currently located in a time zone that is 2 hours ahead of UTC (Coordinated Universal Time), previously known as Greenwich Mean Time (GMT).

当您向操作系统询问从2017-03-14"转换而来的 Date 对象时,您会在您所在的时区获得 2017-03-14 上午午夜的日期/时间参考em> 正确的时间是晚上 10:00 (22:00),然后是 UTC 时间的前一晚.

When you ask the OS for a Date object converted from "2017-03-14" you get a date/time reference of Midnight the morning of 2017-03-14 in your time zone which is, correctly, 10:00 pm (22:00) then night before in UTC.

当您使用 Date() 向操作系统询问 now 的 Date 对象时,您将获得 now 的日期/时间参考> 在您的时区,这将比 UTC 早两个小时.

When you ask the OS for a Date object for now with Date() you get a date/time reference of now in your time zone, which will be two hours earlier in UTC.

要准确评估您的日期字符串以说现在现在早于 'tonight of 2017-03-14'",您可能需要从2017-03-14 23:59"转换(或晚上 11:59,或者可能在今晚 8:00 pm 等活动开始之前).

To accurately evaluate your date string to say "is now earlier than 'tonight of 2017-03-14'" you will probably want to convert from "2017-03-14 23:59" (or 11:59 pm, or perhaps prior to the start of tonight's event of 8:00 pm, etc).

这将进行您的原始比较,但作为函数会更好地工作(尽管我不确定您想如何使用它)...

This will do your original comparison, but would work better as a function (although I'm not sure how you want to use it)...

var newDateString : String = ""

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"

// set tonightDate to 1 minute before midnight, tonight, in local time
if let tonightDate = dateFormatter.date(from: "2017-03-14 23:59") {

    // set nowDate to current local time
    let nowDate = Date()

    let comp = nowDate.compare(tonightDate)

    if comp.rawValue <= 0 {
        newDateString = "TONIGHT"
    } else {
        dateFormatter.dateFormat = "EEEE, MMMM d, yyyy"
        newDateString = dateFormatter.string(from: tonightDate)
    }

}

print(newDateString)

这篇关于将字符串转换为返回前一天的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆