Search WEB date only

Asked

Viewed 36 times

0

I need to search the date acts from the Internet, in the code below the return is the date, hour, minutes and seconds.

public static DateTime GetNistTime()
    {
        var myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
        var response = myHttpWebRequest.GetResponse();
        string todaysDates = response.Headers["date"];
        return DateTime.ParseExact(todaysDates,
                                   "ddd, dd MMM yyyy HH:mm:ss 'GMT'",
                                   CultureInfo.InvariantCulture.DateTimeFormat,
                                   DateTimeStyles.AssumeUniversal);
    }

As I search only the date, since just deleting the section HH:mm:ss doesn’t work?

Return format:

Foto

  • 1

    What if you just convert to datetime? Then you can convert to any format you want. What format do you expect?

  • @Aline I edited the code

  • 1

    What format do you want? dd/MM/yyyy??

  • Yes: dd/MM/yyyy

  • 1

    Young man, your code is right, man. Show me the part that does the MessageBox.Show

  • MessageBox.Show(GetNistTime().ToString());

  • 1

    Messagebox.Show(Getnisttime(). Toshortdatestring());

  • 1

    And, tell me, what is your intention? Show in Messagebox without the time?

  • The intention is to save the current date, ex: 05/30/2017 in the database

  • 1

    And why don’t you save her then?

  • 'Cause I’m gonna need to compare bank dates to the current date, and time, in which case it’s gonna mess me up, I got the answer down there, thank you!

  • @jbueno And as I add days to save to database, for example today is day 25, adding 5 days, would day 30?

  • 1

    data.AddDays(5)

Show 8 more comments

2 answers

2

Edit:

You can keep your method as it is, and when calling it, just ignore the hours:

public static DateTime GetNistTime()
    {
        var myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
        var response = myHttpWebRequest.GetResponse();
        string todaysDates = response.Headers["date"];
        return DateTime.ParseExact(todaysDates,
                                   "ddd, dd MMM yyyy HH:mm:ss 'GMT'",
                                   CultureInfo.InvariantCulture.DateTimeFormat,
                                   DateTimeStyles.AssumeUniversal);
    }

Calling the method:

label1.Text = GetNistTime().ToShortDateString();
  • In this way the return continues with the hour, but zeroed, in this way: 30/05/2017 00:00:00

  • Cannot convert to String a variable System.DateTime

  • 1

    All Datetime (as the name says), will have date and time. So in the second example, the return type of the method was changed to string, and I started to return only to Data

  • 1

    but I recommend that you use it in its initial form, returning the hours, then at the time of calling the method you can put the . Toshortdatestring(); and ignore the hours that came in the result

2


public static string GetNistTime()
    {
        var myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
        var response = myHttpWebRequest.GetResponse();
        string todaysDates = response.Headers["date"];
        return DateTime.Parse(todaysDates).toString("dd/MM/yyyy");
    }
  • Cannot convert to String a variable System.DateTime

  • 1

    You will need to change the return of the method and the place where you seek that information. Or, keep returning datetime in the method and do toString(".") where you get the return of this method.

  • I put it the same way it is here.

  • Made the same mistake: Cannot implicitly convert type 'string' to 'System.DateTime'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.