-
Feb 27th, 2018, 05:39 AM
#1
Thread Starter
Addicted Member
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.
-
Feb 27th, 2018, 06:23 AM
#2
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!
-
Feb 27th, 2018, 06:25 AM
#3
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.
-
Feb 27th, 2018, 06:38 AM
#4
Thread Starter
Addicted Member
Re: Get current Date & Time based on system date & time format ?
Originally Posted by TysonLPrice
Get it off a database or off the web.
Any reliable permanent source to get date time from ?
-
Feb 27th, 2018, 06:38 AM
#5
Thread Starter
Addicted Member
Re: Get current Date & Time based on system date & time format ?
Originally Posted by ChrisE
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
-
Feb 27th, 2018, 06:43 AM
#6
Re: Get current Date & Time based on system date & time format ?
Originally Posted by GlowingVB
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|