OBTER DATA ATUAL ATRAVES DA INTERNET

ADHEL 25/01/2011 13:38:10
#363364
Como o titulo já diz
precisava obter a data atual de algum site e jogar num label
alguem tem algum exemplo ?
PEGUDO 26/01/2011 08:32:59
#363453
Resposta escolhida
primeiro precisamos saber qual site voce vai acessar visto que cada site tem controles que ficam em lugares diferentes e tem nomes diferentes.
TECLA 26/01/2011 16:37:15
#363514
Escrevendo uma rotina para buscar TAGs específicas no fonte HTML da página abaixo, dá pra se obter a data e hora sem problemas.
http://www.horariodebrasilia.org/
FMVC 26/01/2011 19:34:13
#363531
Tente usar isso:



 Shell([Ô]NET TIME \\123.456.789.0 /SET /YES[Ô] , vbhide)  


Onde 132.456.789.0 é o ip ou nome do Servidor
ADHEL 27/01/2011 09:02:34
#363563
Citação:

:
primeiro precisamos saber qual site voce vai acessar visto que cada site tem controles que ficam em lugares diferentes e tem nomes diferentes.


Qualquer um site serve, desde que a data apareça assim 27/01/2010 ou 27/1/2010

Citação:

:
Escrevendo uma rotina para buscar TAGs específicas no fonte HTML da página abaixo, dá pra se obter a data e hora sem problemas.
http://www.horariodebrasilia.org/



isso eu nunca fiz,seria possível disponibilizar um exemplo
PEGUDO 27/01/2011 09:53:04
#363575
Cara fiz este exemplo aqui, baseado na página que o TECLA deu como exemplo.
Lembre-se que neste exemplo você precisa de:
1. um WebBrowser em seu formulário que pode ficar escondido para que o usuário não possa vê-lo.
2. Um Label
3. Um Timer

[txt-color=#0000f0]Public Class [/txt-color]Form1

[txt-color=#0000f0] Private Sub [/txt-color]Form1_Load([txt-color=#0000f0]ByVal[/txt-color] sender [txt-color=#0000f0]As[/txt-color] System.Object, [txt-color=#0000f0]ByVal[/txt-color] e [txt-color=#0000f0]As[/txt-color] System.EventArgs)[txt-color=#0000f0] Handles [/txt-color]MyBase.Load
WebBrowser1.Navigate([txt-color=#e80000][Ô][/txt-color][txt-color=#0000f0]http://www.horariodebrasilia.org/[/txt-color][txt-color=#e80000][Ô][/txt-color])

[txt-color=#0000f0] While Not [/txt-color]WebBrowser1.ReadyState = WebBrowserReadyState.Complete
Application.DoEvents()
[txt-color=#0000f0]End While[/txt-color]

Timer1.Enabled = [txt-color=#0000f0]True[/txt-color]
[txt-color=#0000f0]End Sub[/txt-color]

[txt-color=#0000f0]Private Sub[/txt-color] Timer1_Tick([txt-color=#0000f0]ByVal[/txt-color] sender [txt-color=#0000f0]As[/txt-color] System.Object, [txt-color=#0000f0]ByVal[/txt-color] e [txt-color=#0000f0]As[/txt-color] System.EventArgs) [txt-color=#0000f0]Handles[/txt-color] Timer1.Tick
[txt-color=#007100][ô][Ô]day[Ô] é o nome do span contido no html da página[/txt-color]
[txt-color=#0000f0]Dim[/txt-color] Dia [txt-color=#0000f0]As[/txt-color] HtmlElement = WebBrowser1.Document.GetElementById([txt-color=#e80000][Ô]day[Ô][/txt-color])

[txt-color=#007100][ô][Ô]time-footer[Ô] é o nome do span contido no html da página[/txt-color]
[txt-color=#0000f0]Dim[/txt-color] Hora [txt-color=#0000f0]As[/txt-color] HtmlElement = WebBrowser1.Document.GetElementById([txt-color=#e80000][Ô]time-footer[Ô][/txt-color])
[txt-color=#0000f0]Dim[/txt-color] Data [txt-color=#0000f0]As String[/txt-color] = Dia.InnerText
[txt-color=#0000f0]Dim[/txt-color] Horario [txt-color=#0000f0]As String[/txt-color] = Hora.InnerText

Tratar_Dia(Data)

Label1.Text = [Ô]Hoje é dia: [Ô] & Data & Chr(13) & [txt-color=#e80000][Ô]Hora certa: [Ô][/txt-color] & Horario & [txt-color=#e80000][Ô] (horário de Brasília)[Ô][/txt-color]
[txt-color=#0000f0]End Sub

Private Sub[/txt-color] Tratar_Dia([txt-color=#0000f0]ByRef[/txt-color] QualDia [txt-color=#0000f0]As String[/txt-color])
[txt-color=#0000f0]Dim[/txt-color] Vetor() [txt-color=#0000f0]As String[/txt-color] = Split(QualDia, [txt-color=#e80000][Ô] [Ô][/txt-color])
[txt-color=#0000f0]Dim[/txt-color] Mes [txt-color=#0000f0]As String[/txt-color] = TimeOfDay.Month

[txt-color=#0000f0]If[/txt-color] Vetor(2).Length = 1 [txt-color=#0000f0]Then[/txt-color] Vetor(2) = [txt-color=#e80000][Ô]0[Ô][/txt-color] & Vetor(2)
[txt-color=#0000f0]If[/txt-color] Mes.Length = 1 [txt-color=#0000f0]Then[/txt-color] Mes = [txt-color=#e80000][Ô]0[Ô][/txt-color] & Mes

QualDia = Replace(Vetor(2), [txt-color=#e80000][Ô],[Ô][/txt-color], [txt-color=#e80000][Ô][Ô][/txt-color]) & [txt-color=#e80000][Ô]/[Ô][/txt-color] & Mes & [txt-color=#e80000][Ô]/[Ô][/txt-color] & Vetor(3)
[txt-color=#0000f0]End Sub
End Class[/txt-color]

Coloquei o projeto em ANEXO também, caso queira baixar e ver o resultado
RODRIGOFERRO 27/01/2011 09:54:18
#363576
eu achei uma classe que faz isso...

Ela se baseia em varios Ips, e vai tentando até achar a hora em algum, se nao achar em um ele vai tentar em outro... excelente classe viu...

Maravilhosa !


Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Runtime.InteropServices

Public Class Daytime
[ô]Internet Time Server class by Alastair Dallas 01/27/04

Private Const THRESHOLD_SECONDS As Integer = 15 [ô]Number of seconds
[ô] that Windows clock can deviate from NIST and still be okay

[ô]Server IP addresses from
[ô]http://www.boulder.nist.gov/timefreq/service/time-servers.html
Private Shared Servers() As String = { _
[Ô]129.6.15.28[Ô] _
, [Ô]129.6.15.29[Ô] _
, [Ô]132.163.4.101[Ô] _
, [Ô]132.163.4.102[Ô] _
, [Ô]132.163.4.103[Ô] _
, [Ô]128.138.140.44[Ô] _
, [Ô]192.43.244.18[Ô] _
, [Ô]131.107.1.10[Ô] _
, [Ô]66.243.43.21[Ô] _
, [Ô]216.200.93.8[Ô] _
, [Ô]208.184.49.9[Ô] _
, [Ô]207.126.98.204[Ô] _
, [Ô]205.188.185.33[Ô] _
}

Public Shared LastHost As String = [Ô][Ô]
Public Shared LastSysTime As DateTime

Public Shared Function GetTime() As DateTime
[ô]Returns UTC/GMT using an NIST server if possible,
[ô] degrading to simply returning the system clock

[ô]If we are successful in getting NIST time, then
[ô] LastHost indicates which server was used and
[ô] LastSysTime contains the system time of the call
[ô] If LastSysTime is not within 15 seconds of NIST time,
[ô] the system clock may need to be reset
[ô] If LastHost is [Ô][Ô], time is equal to system clock

Dim host As String
Dim result As DateTime

LastHost = [Ô][Ô]
For Each host In Servers
result = GetNISTTime(host)
If result > DateTime.MinValue Then
LastHost = host
Exit For
End If
Next

If LastHost = [Ô][Ô] Then
[ô]No server in list was successful so use system time
result = DateTime.UtcNow()
End If

Return result
End Function

Public Shared Function SecondsDifference(ByVal dt1 As DateTime, ByVal dt2 As DateTime) As Integer
Dim span As TimeSpan = dt1.Subtract(dt2)
Return span.Seconds + (span.Minutes * 60) + (span.Hours * 360)
End Function

Public Shared Function WindowsClockIncorrect() As Boolean
Dim nist As DateTime = GetTime()
If (Math.Abs(SecondsDifference(nist, LastSysTime)) > THRESHOLD_SECONDS) Then
Return True
End If
Return False
End Function

Private Shared Function GetNISTTime(ByVal host As String) As DateTime
[ô]Returns DateTime.MinValue if host unreachable or does not produce time
Dim result As DateTime
Dim timeStr As String

Try
Dim reader As New StreamReader(New TcpClient(host, 13).GetStream)
LastSysTime = DateTime.UtcNow()
timeStr = reader.ReadToEnd()
reader.Close()
Catch ex As SocketException
[ô]Couldn[ô]t connect to server, transmission error
Debug.WriteLine([Ô]Socket Exception [[Ô] & host & [Ô]][Ô])
Return DateTime.MinValue
Catch ex As Exception
[ô]Some other error, such as Stream under/overflow
Return DateTime.MinValue
End Try

[ô]Parse timeStr
If (timeStr.Substring(38, 9) <> [Ô]UTC(NIST)[Ô]) Then
[ô]This signature should be there
Return DateTime.MinValue
End If
If (timeStr.Substring(30, 1) <> [Ô]0[Ô]) Then
[ô]Server reports non-optimum status, time off by as much as 5 seconds
Return DateTime.MinValue [ô]Try a different server
End If

Dim jd As Integer = Integer.Parse(timeStr.Substring(1, 5))
Dim yr As Integer = Integer.Parse(timeStr.Substring(7, 2))
Dim mo As Integer = Integer.Parse(timeStr.Substring(10, 2))
Dim dy As Integer = Integer.Parse(timeStr.Substring(13, 2))
Dim hr As Integer = Integer.Parse(timeStr.Substring(16, 2))
Dim mm As Integer = Integer.Parse(timeStr.Substring(19, 2))
Dim sc As Integer = Integer.Parse(timeStr.Substring(22, 2))

If (jd < 15020) Then
[ô]Date is before 1900
Return DateTime.MinValue
End If
If (jd > 51544) Then yr += 2000 Else yr += 1900

Return New DateTime(yr, mo, dy, hr, mm, sc)

End Function

<StructLayout(LayoutKind.Sequential)> _
Public Structure SYSTEMTIME
Public wYear As Int16
Public wMonth As Int16
Public wDayOfWeek As Int16
Public wDay As Int16
Public wHour As Int16
Public wMinute As Int16
Public wSecond As Int16
Public wMilliseconds As Int16
End Structure

Private Declare Function GetSystemTime Lib [Ô]kernel32.dll[Ô] (ByRef stru As SYSTEMTIME) As Int32
Private Declare Function SetSystemTime Lib [Ô]kernel32.dll[Ô] (ByRef stru As SYSTEMTIME) As Int32

Public Shared Sub SetWindowsClock(ByVal dt As DateTime)
[ô]Sets system time. Note: Use UTC time; Windows will apply time zone

Dim timeStru As SYSTEMTIME
Dim result As Int32

timeStru.wYear = CType(dt.Year, Int16)
timeStru.wMonth = CType(dt.Month, Int16)
timeStru.wDay = CType(dt.Day, Int16)
timeStru.wDayOfWeek = CType(dt.DayOfWeek, Int16)
timeStru.wHour = CType(dt.Hour, Int16)
timeStru.wMinute = CType(dt.Minute, Int16)
timeStru.wSecond = CType(dt.Second, Int16)
timeStru.wMilliseconds = CType(dt.Millisecond, Int16)

result = SetSystemTime(timeStru)

End Sub
End Class



Chame ela assim:

Dim TesteClasse As DateTime = Daytime.GetTime

Só precisamos achar um ip de um servidor brazuca..

ADHEL 27/01/2011 10:33:40
#363578
Obrigado a todos pela atenção dispensada
PEGUDO seu exemplo foi EXCELENTE
VALEU
Tópico encerrado , respostas não são mais permitidas