0

I want to get date and time from database then use it to compare with current date and time. I'm able to get time from database and compare it with the computer's time but I want to use a specific time which is same for everyone. Basically I want to get time from web. I couldn't figure NTP and SNTP

if (myReader.HasRows)
{
    myReader.Read();
    var expiryDate = myReader.GetDateTime("expirationdate");
    if (DateTime.UtcNow.AddHours(2) > expiryDate)
    {
    }
    else
    {
    }
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • What is your question exactly? Why do you think using `UtcNow` is not right? Can you please be more specific? There are some way to calculate NTP time as well. http://stackoverflow.com/q/1193955/447156 – Soner Gönül Nov 18 '15 at 08:43
  • @SonerGönül - I think he wants to get the Date from Internet. may be he needs tp go throw web services. – Emad - Developer Nov 18 '15 at 08:46
  • You want database `datetime` compare with any user `local datetime` depends from timezone?. – ebattulga Nov 18 '15 at 08:48
  • You can find how to Get the time from the Web here: http://stackoverflow.com/questions/6435099/how-to-get-datetime-from-the-internet – Stefano Bafaro Nov 18 '15 at 08:48
  • @SonerGönül I'm not sure is it allowed to write in foreign language but i'll try my chance. "Bu kodu kullanıcı giriş yaparken kullanıyorum ve ücretli,süreli üyelik sistemim var veritabanından zamanı kontrol ediyor ve giriş hakkı veriyor ama kullanıcı bilgisayar saatini geri alırsa süresi bitmiş olsa bile giriş yapabiliyor bunu engellemek için saati internet üzerinden çekmek istiyorum NTP ve SNTP ile nasıl yapabileceğimi çözemedim." Your profile shows you're from Istanbul then you can speak Turkish. – FlyingMadman Nov 18 '15 at 08:50

2 Answers2

0
void Main()
{
  string result; 
  using (var con = new OleDbConnection(@"..."))
  {
    var cmd = new OleDbCommand("select now()",con);
    con.Open();
    result = (string)cmd.ExecuteScalar();
    con.Close();
  }

  DateTime now = DateTime.ParseExact(result, "yyyy-MM-dd HH:mm:ss", null);
  Console.WriteLine(now);
}
Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39
  • This is the error I'm getting System.DateTime' türündeki nesne 'System.String' türüne atılamadı. – FlyingMadman Nov 18 '15 at 14:28
  • MySQL documentation claims that it returns a string. It is you who use MySQL, and it is better if it is returning a DateTime, you don't need the casting from string and can directly use the DateTime. – Cetin Basoz Nov 18 '15 at 17:29
-1

Reading your Turkish explanation, and assuming "database" is MS SQL Server (I don't understand why people don't tell what the "database" is), you can directly get the date and time from your database. If you mean the database server is the local computer then for getting time from internet, check:

Get Datetime from internet

Community
  • 1
  • 1
Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39
  • It's MySQL I've found this question before but I couldn't figure it out. Is there any other guide line for this ? – FlyingMadman Nov 18 '15 at 10:03
  • What part of it you couldn't figure out? It is very simple. You can query current timestamp from MySQL, no? For example you could do: new DateTime.ParseExact( (string)OleDbCommand( "select now()", yourConnection ).ExecuteScalar(), "yyyy-MM-dd HH:mm:ss"). – Cetin Basoz Nov 18 '15 at 11:23
  • I don't use MySQL but postgreSQL and SQL server, didn't think of MySQLConnection, you can use that. OleDb is universal, you can use with any database that has OleDb provider. – Cetin Basoz Nov 18 '15 at 11:54
  • Btw I get this error The type name 'ParseExact' does not exist in the type 'DateTime'. Then I'll change my whole my connection code as OleDb. and i'm using Reader Does OleDb support it? – FlyingMadman Nov 18 '15 at 11:56
  • ParseExact doesn't exist ????? Which version of .Net are you using (as far as I know it even existed back in .Net 1.0). Probably you are doing something wrong, I will give a sample with OleDb (leaving connection string part empty), you can continue from there. – Cetin Basoz Nov 18 '15 at 12:11