WP7 - 格式化DateTime。怎么样?

时间:2013-11-27 10:58:50

标签: vb.net windows-phone-7 date format

我想知道是否有办法将日期时间格式化为我想要的方式。所以基本上我的场景是,当我的应用从隔离存储中获取文件时,它将日期显示为“小时:分钟”。我想要它,以便当应用程序获得日期时显示为“小时:日期”,但是在一天之后它将其显示为“日/月”我该怎么做?

到目前为止,这就是我所得到的:

Dim Storage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
        Dim directory As String = "./MyNote/SavedNotes/*.*"
        Dim filenames As String() = Storage.GetFileNames(directory)
        Dim dataSource As New List(Of SampleData)()
        For Each filename As String In filenames
            Dim ISF As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
            Dim FS As IsolatedStorageFileStream = ISF.OpenFile("MyNote/SavedNotes/" & filename, FileMode.Open, FileAccess.Read)
            Using SR As New StreamReader(FS)
                Dim FTime As String = Storage.GetCreationTime("MyNote/SavedNotes/" & filename).ToString("d/M ddd")
                Dim DATESS As String = SR.ReadLine
                Dim ReadName As String = SR.ReadLine
                dataSource.Add(New SampleData() With {.FileNameX = filename, .Description = ReadName, .FileTime = FTime})
            End Using
            SavedNotesList.ItemsSource = dataSource
        Next

或多或少像本机消息传递应用程序。日期格式取决于收到邮件的时间。

1 个答案:

答案 0 :(得分:3)

您可能需要为这些创建一些实用程序功能...手机上日期和时间的格式取决于手机的文化和本地化。下面代码中的某些内容是一个开始。如果您需要VB示例,请告诉我,可以找到日期格式文档here

        /// <summary>
    /// Get's a date description
    /// </summary>
    /// <param name="dateTime">
    /// The date to compare
    /// </param>
    /// <returns>
    /// A date description.
    /// </returns>
    public string GetRelativeTime(DateTime dateTime)
    {
        var timeMode = GetTimeMode(dateTime);

        switch (timeMode)
        {
          case TimeMode.Seconds:
          case TimeMode.Minutes:
          case TimeMode.Hours:
              return dateTime.ToString("HH:mm");
            default:
              return dateTime.ToString("dd/MM");;
        }
    }

    /// <summary>
    /// Gets a date mode.
    /// </summary>
    /// <param name="dateTime">
    /// The date to test.
    /// </param>
    /// <returns>
    /// A date mode.
    /// </returns>
    public TimeMode GetTimeMode(DateTime dateTime)
    {
        var timeSpan = DateTime.Now - dateTime;

        if (timeSpan < TimeSpan.FromSeconds(60)) return TimeMode.Seconds;
        if (timeSpan < TimeSpan.FromMinutes(60)) return TimeMode.Minutes;
        if (timeSpan < TimeSpan.FromHours(24)) return TimeMode.Hours;
        if (timeSpan <= TimeSpan.FromDays(30)) return TimeMode.Days; //this can be tricky for days in month, adapt as appropriate.
        if (timeSpan.Days <= 365) return TimeMode.Months; //adapt as apropriate for leap years.

        return TimeMode.Years;
    }

    /// <summary>
    /// Date Modes.
    /// </summary>
    public enum TimeMode
    {
        Seconds,
        Minutes,
        Hours,
        Days,
        Weeks,
        Months,
        Years
    }

使用以下

进行测试
MessageBox.Show(GetRelativeTime(DateTime.Now.Subtract(new TimeSpan(0, 0, 0, 20)))); //20 seconds ago
        MessageBox.Show(GetRelativeTime(DateTime.Now.Subtract(new TimeSpan(0, 0, 20, 0)))); //20 minutes ago
        MessageBox.Show(GetRelativeTime(DateTime.Now.Subtract(new TimeSpan(0, 20, 0, 0)))); //20 hours ago
        MessageBox.Show(GetRelativeTime(DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0)))); //1 day ago
        MessageBox.Show(GetRelativeTime(DateTime.Now.Subtract(new TimeSpan(2, 0, 0, 0)))); //2 days ago

编辑:添加了VB代码

Dim Storage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
    Dim directory As String = "./MyNote/SavedNotes/*.*"
    Dim filenames As String() = Storage.GetFileNames(directory)
    Dim dataSource As New List(Of SampleData)()
    For Each filename As String In filenames
        Dim ISF As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
        Dim FS As IsolatedStorageFileStream = ISF.OpenFile("MyNote/SavedNotes/" & filename, FileMode.Open, FileAccess.Read)
        Using SR As New StreamReader(FS)
            'Modified code
            Dim FTime As String = GetRelativeTime(Storage.GetCreationTime("MyNote/SavedNotes/" & filename))
            Dim DATESS As String = SR.ReadLine
            Dim ReadName As String = SR.ReadLine
            dataSource.Add(New SampleData() With {.FileNameX = filename, .Description = ReadName, .FileTime = FTime})
        End Using
        SavedNotesList.ItemsSource = dataSource
    Next

'新代码

''' <summary>
''' Get's a date description
''' </summary>
''' <param name="dateTime">
''' The date to compare
''' </param>
''' <returns>
''' A date description.
''' </returns>
Public Function GetRelativeTime(dateTime As DateTime) As String
Dim myTimeMode = GetTimeMode(dateTime)

Select Case myTimeMode
    Case TimeMode.Seconds, TimeMode.Minutes, TimeMode.Hours
        Return dateTime.ToString("HH:mm")
    Case Else
        Return dateTime.ToString("dd/MM")
End Select
End Function

''' <summary>
''' Gets a date mode.
''' </summary>
''' <param name="dateTime">
''' The date to test.
''' </param>
''' <returns>
''' A date mode.
''' </returns>
Public Function GetTimeMode(timeToTest As DateTime) As TimeMode
Dim timeDifference = DateTime.Now - timeToTest

If timeDifference < TimeSpan.FromSeconds(60) Then
    Return TimeMode.Seconds
End If
If timeDifference < TimeSpan.FromMinutes(60) Then
    Return TimeMode.Minutes
End If
If timeDifference < TimeSpan.FromHours(24) Then
    Return TimeMode.Hours
End If
If timeDifference <= TimeSpan.FromDays(30) Then
    Return TimeMode.Days
End If
'this can be tricky for days in month, adapt as appropriate.
If timeDifference.Days <= 365 Then
    Return TimeMode.Months
End If
'adapt as apropriate for leap years.
Return TimeMode.Years
End Function

''' <summary>
''' Date Modes.
''' </summary>
Public Enum TimeMode
Seconds
Minutes
Hours
Days
Weeks
Months
Years
End Enum