Reputation: 487
I was wondering if there is a way to format datetime to the way i want it. So basically my scenarios is that when my app gets a file from the isolated storage it shows the date as "Hours:Minutes". I want it so that when the app gets the date it shows as "Hours:Date" but after a day it shows it as "Day/Month" how can i do this?
Soo far this is what i got:
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
More or less like the native messaging app. The date formats depending on when the message was received.
Upvotes: 0
Views: 188
Reputation: 2121
You might need to create some utility functions for these... the format of the date and time on your phone depends on the culture and localization of your phone. Something along the line of the code below is a start. let me know if you need VB examples, date formats documentation can be found 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
}
tests with the following
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
EDIT: added VB Code
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
'New Code
''' <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
Upvotes: 3