How to Convert a String to a Date in C#

Asked

Viewed 21 times

1

I am trying to build a method that takes 4 parameters: "name" which will be a text, "data_birth" which will be a date, "profession" which will be a text and "numero_calcado" which will be an integer. The Method should return a text with the phrase (example) "Bruno, born on 02/18/1980, is programmer and pants 46.".

However, I cannot convert Date String to Date Format.

I tried Convert.toDateTime but it wasn’t.

How the right way would be?

I tried that way:

  //Data de Nascimento
    Console.Write("Insira a Data de Nascimento do Cliente: ");
    string data_nascimento = Console.ReadLine();  
    Convert.ToDateTime(data_nascimento);

I tried again that way:

//Data de Nascimento
  Console.Write("Insira a Data de Nascimento do Cliente: ");
  string data_string = Console.ReadLine();  
  DateTime data_nascimento = DateTime.Parse(data_string);

I was able to convert, but the Console returned to Time after Date and I don’t want the time, how can I remove?

My questions are being closed almost immediately by the moderators and I’m not understanding why, I started participating here today.

Thank you!

No answers

Browser other questions tagged

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