开发者

Displaying a date with time using region settings formating

I expected this to work:

Public Shared Function ToShortDateTimeString(ByVal dateIn As Date) As String
    Return dateIn.ToShortDateString & " " & dateIn.ToShortTimeString
End Function

The date gets formatted according to local settings, but not the time.

I seen API examples but, dangit, this should be easy and built into the framework.

Why is it working for the date but not the time?

For example, check out my regional settings for displaying time:

http://www.screencast.com/users/Dokmanc/folders/Jing/media/cbc07eeb-1f9c-4b27-b535-91b开发者_开发技巧8acbffd8e

However, the above function returns: "11/17/2009 8:29:32 PM"


You can to use DateTimeFormatInfo to force your desired date/time format:

DateTimeFormatInfo info = new DateTimeFormatInfo();
info.ShortTimePattern = "HH:mm";

DateTime.Now.ToString(info.ShortTimePattern);

EDIT: Please check this previous question: Why doesn’t DateTime.ToShortTimeString() respect the Short Time format in “Regional and Language Settings”?


Public Shared Function ToShortDateTimeString(ByVal dateIn As Date) As String

       // g = short date and short time
       Return dateIn.ToString("g", System.Globalization.DateTimeFormatInfo.CurrentInfo);

End Function


Sounds like a .NET issue.

Workaround could be:

dateIn.ToString("T", CultureInfo.CurrentUICulture.DateTimeFormat)


Building on what dmitriy said:

To specify the formatting see http://msdn.microsoft.com/en-us/library/az4se3k1(VS.96).aspx

This should illustrate different formattings:

  Dim myDateTime As New DateTime(2001, 5, 16, 3, 2, 15)
  Console.WriteLine(myDateTime.ToString("d", CultureInfo.CurrentUICulture.DateTimeFormat))
  Console.WriteLine(myDateTime.ToString("t", CultureInfo.CurrentUICulture.DateTimeFormat))
  Console.WriteLine(myDateTime.ToString("d", CultureInfo.CreateSpecificCulture("ro-RO")))
  Console.WriteLine(myDateTime.ToString("t", CultureInfo.CreateSpecificCulture("ro-RO")))
  Console.WriteLine(myDateTime.ToString("t", CultureInfo.CurrentUICulture.DateTimeFormat))
  Console.WriteLine(myDateTime.ToShortDateString())
  Console.WriteLine(myDateTime.ToShortTimeString())

In your particular case I think that what you need is:

Return dateIn.ToString("d", CultureInfo.CurrentUICulture.DateTimeFormat) & " " & dateIn.ToString("t", CultureInfo.CurrentUICulture.DateTimeFormat )


Try this:

Option Strict On
Option Explicit On

Imports Microsoft.VisualBasic
Imports System.Globalization

<System.Runtime.CompilerServices.Extension()> _
Module DateTimeExtensions

    <System.Runtime.CompilerServices.Extension()> _
    Public Function ToShortTimeStringNoSeconds(ByVal ShortTimeIn As DateTime) As String

        Dim DateTimeFormat As DateTimeFormatInfo = CultureInfo.CurrentCulture.DateTimeFormat
        Dim ShortTimePattern As String = DateTimeFormat.LongTimePattern.Replace(":ss", String.Empty)

        Return ShortTimeIn.ToString(ShortTimePattern)

    End Function 'GetShortTimeString

End Module

Dim  RegionallyFormattedTime As String = Now.ToShortTimeStringNoSeconds
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜