如何将时间戳转换为用户友好的时间字符串

发布于 2024-08-30 14:56:20 字数 273 浏览 5 评论 0原文

我希望能够在我的申请中显示“今天”和“昨天”作为最近的日期。我目前正在使用一个日期格式化程序来显示日期(从数据记录中检索),并且将继续使用它超过几天的时间。我真的很喜欢 iPhone 中的短信应用程序显示最近消息日期的方式,并且想模仿这一点。

我必须使用的时间戳是在手机下载数据记录的服务器上生成的。因此,所有时间均按 UTC(即 GMT)时间生成。

我已经为此摆弄了一段时间,我设计的解决方案似乎非常冗长。

谁能建议如何实现可以做到这一点的方法?

干杯 - 史蒂夫。

I want to be able to present "today" and "yesterday" for recent dates in my application. I've got a date formatter in use currently to show dates (retrieved from data records) and will keep using this for anything more than a couple of days old. I just really like the way the SMS app in the iPhone shows dates for recent messages and would like to emulate this.

The time-stamps that I have to work with are generated on a server that the phone downloads the data records from. All times are therefore generated at UTC (i.e. GMT) time.

I've been fiddling about with this for a while the solutions I've devised just seem horribly long-winded.

Can anyone suggest how to implement a method that could do this?

Cheers - Steve.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

第七度阳光i 2024-09-06 14:56:20

如果这是一个网络应用程序,您可能会发现 PrettyDate 很有用。我制作了一个 vb.net 实现,可以轻松转换为另一种语言:

Public Function formatDate(ByVal time As DateTime) As String
    Dim datediff As TimeSpan = Now.Subtract(time)

    Dim days As Integer = datediff.TotalDays

    If days < 1 Then
        Dim seconds As Integer = datediff.TotalSeconds
        Select Case seconds
            Case 0 To 60
                Return "just now"
            Case 61 To 120
                Return "1 minute ago"
            Case 121 To 3600
                Return Math.Floor(seconds / 60) & " minutes ago"
            Case 3601 To 7200
                Return "1 hour ago"
            Case 7201 To 86400
                Return Math.Floor(seconds / 3600) & " hours ago"
        End Select
    ElseIf days < 31 Then
        Select Case days
            Case 1
                Return "yesterday"
            Case 2 To 7
                Return days & " days ago"
            Case Is > 7
                Return Math.Ceiling(days / 7) & " weeks ago"
        End Select
    Else : Return time.ToString("MM/dd/yyyy")
    End If
End Function

If this is a web app, you might find PrettyDate useful. I made a vb.net implementation that could easily be converted to another language:

Public Function formatDate(ByVal time As DateTime) As String
    Dim datediff As TimeSpan = Now.Subtract(time)

    Dim days As Integer = datediff.TotalDays

    If days < 1 Then
        Dim seconds As Integer = datediff.TotalSeconds
        Select Case seconds
            Case 0 To 60
                Return "just now"
            Case 61 To 120
                Return "1 minute ago"
            Case 121 To 3600
                Return Math.Floor(seconds / 60) & " minutes ago"
            Case 3601 To 7200
                Return "1 hour ago"
            Case 7201 To 86400
                Return Math.Floor(seconds / 3600) & " hours ago"
        End Select
    ElseIf days < 31 Then
        Select Case days
            Case 1
                Return "yesterday"
            Case 2 To 7
                Return days & " days ago"
            Case Is > 7
                Return Math.Ceiling(days / 7) & " weeks ago"
        End Select
    Else : Return time.ToString("MM/dd/yyyy")
    End If
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文