Results 1 to 6 of 6

Thread: Get current Date & Time based on system date & time format ?

  1. #1

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Question Get current Date & Time based on system date & time format ?

    Greetings.

    Here is the most common way to get current date and time :
    Code:
    DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
    But date or time format may differ in each machine. So I need a code like the above one to show current date and time with the same format of date and time of the machine. I guess it works with
    Code:
    DateTime.Now.ToString
    , but does it differ concerning short or long date time format ? ( does it include both short and long changes of date time format of machine?)

    Thanks in advance
    Last edited by GlowingVB; Feb 27th, 2018 at 05:51 AM.

  2. #2
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,834

    Re: Get current Date & Time based on system date & time format ?

    I wouldn't be getting the date and time off of the users machine in the first place if it is used for anything meaningful; like auditing. The user can set the date\time to whatever they want. Back in the day that was a trick to keep using trail software. Get it off a database or off the web.
    Please remember next time...elections matter!

  3. #3
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: Get current Date & Time based on system date & time format ?

    Hi,

    take a look at Culture.Info and System.Globalization

    here also a few samples...
    Code:
    Imports System.Globalization
    
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'get the Culture first
            Dim culture As CultureInfo = CultureInfo.CurrentCulture
            TextBox1.Text = culture.NativeName & " ," & culture.DateTimeFormat.FullDateTimePattern
    
    
            'here a few samples
            Label2.Text = _Test_SetDateTimeFormat(CStr(1))
            Label3.Text = _Test_SetDateTimeFormat(CStr(2))
            Label4.Text = _Test_SetDateTimeFormat(CStr(3))
            Label5.Text = _Test_SetDateTimeFormat(CStr(4))
        End Sub
    
        Private Function _Test_SetDateTimeFormat(ByVal Index As String) As String
            Dim dt As Date = Now()
            Select Case Index
                Case CStr(1)
                    Dim x As CultureInfo = New CultureInfo("fr-FR")
                    Return dt.ToString("D", x.DateTimeFormat)
                Case CStr(2)
                    Dim x As CultureInfo = New CultureInfo("en-US")
                    Return dt.ToString("D", x.DateTimeFormat)
                Case CStr(3)
                    Dim x As CultureInfo = New CultureInfo("de-DE")
                    Return dt.ToString("D", x.DateTimeFormat)
                Case CStr(4)
                    Dim x As CultureInfo = New CultureInfo("ar-OM")
                    Return dt.ToString("D", x.DateTimeFormat)
            End Select
        End Function
    
    End Class
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  4. #4

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: Get current Date & Time based on system date & time format ?

    Quote Originally Posted by TysonLPrice View Post
    Get it off a database or off the web.
    Any reliable permanent source to get date time from ?

  5. #5

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: Get current Date & Time based on system date & time format ?

    Quote Originally Posted by ChrisE View Post
    Hi,

    take a look at Culture.Info and System.Globalization

    here also a few samples...
    Code:
    Imports System.Globalization
    
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'get the Culture first
            Dim culture As CultureInfo = CultureInfo.CurrentCulture
            TextBox1.Text = culture.NativeName & " ," & culture.DateTimeFormat.FullDateTimePattern
    
    
            'here a few samples
            Label2.Text = _Test_SetDateTimeFormat(CStr(1))
            Label3.Text = _Test_SetDateTimeFormat(CStr(2))
            Label4.Text = _Test_SetDateTimeFormat(CStr(3))
            Label5.Text = _Test_SetDateTimeFormat(CStr(4))
        End Sub
    
        Private Function _Test_SetDateTimeFormat(ByVal Index As String) As String
            Dim dt As Date = Now()
            Select Case Index
                Case CStr(1)
                    Dim x As CultureInfo = New CultureInfo("fr-FR")
                    Return dt.ToString("D", x.DateTimeFormat)
                Case CStr(2)
                    Dim x As CultureInfo = New CultureInfo("en-US")
                    Return dt.ToString("D", x.DateTimeFormat)
                Case CStr(3)
                    Dim x As CultureInfo = New CultureInfo("de-DE")
                    Return dt.ToString("D", x.DateTimeFormat)
                Case CStr(4)
                    Dim x As CultureInfo = New CultureInfo("ar-OM")
                    Return dt.ToString("D", x.DateTimeFormat)
            End Select
        End Function
    
    End Class
    regards
    Chris
    that's cool to get it based on cultures (Culture.Info) . Thanks

  6. #6
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,834

    Re: Get current Date & Time based on system date & time format ?

    Quote Originally Posted by GlowingVB View Post
    Any reliable permanent source to get date time from ?
    There are lots of them. Do a search on "vb .net get date and time off internet". We use our database since we control it.
    Please remember next time...elections matter!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width