0

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-91b8acbffd8e

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

Chad
  • 23,658
  • 51
  • 191
  • 321
  • Might be helpful to tell us what you actually get and what you want, for example. – Noldorin Nov 18 '09 at 01:30
  • possible duplicate: http://stackoverflow.com/questions/1292246/why-doesnt-datetime-toshorttimestring-respect-the-short-time-format-in-region – Rubens Farias Nov 18 '09 at 01:31

5 Answers5

3

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”?

Community
  • 1
  • 1
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
1
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
user207331
  • 69
  • 3
0

Sounds like a .NET issue.

Workaround could be:

dateIn.ToString("T", CultureInfo.CurrentUICulture.DateTimeFormat)
Dmytrii Nagirniak
  • 23,696
  • 13
  • 75
  • 130
0

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 )

Ando
  • 11,199
  • 2
  • 30
  • 46
0

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
Chad
  • 23,658
  • 51
  • 191
  • 321