1

I have a DateTime, i'm trying to convert it to string, then convert it back to DateTime to format it. But i keep getting "string isn't recognized as valid datetime string". Any ideas?

var data = list_2.Entries.Select(c => new clients { code = cli_code, last_backup = c.Name, last_backup_date = c.AsFile.ClientModified.ToLocalTime() }).LastOrDefault(); 
var last_backup_date = data.last_backup_date;
var last_backup_date_string = Convert.ToString(last_backup_date);
var last_backup_date_formatted = DateTime.ParseExact(last_backup_date_string, "dd/MM/yyyy HH:MM:ss", CultureInfo.InvariantCulture);
var today_date = DateTime.Today;
10
  • Can you add value of last_backup_date_string? Commented Jun 23, 2020 at 15:16
  • yeah, sure, one min Commented Jun 23, 2020 at 15:17
  • i can put the output from the data.last_backup_date if you'd like Commented Jun 23, 2020 at 15:18
  • 1
    we need to see an example of how last_backup_date looks like Commented Jun 23, 2020 at 15:20
  • What is the actual datatype of var last_backup_date = data.last_backup_date; since you need to convert it to string with this line Convert.ToString(last_backup_date); ? Commented Jun 23, 2020 at 15:21

3 Answers 3

3

Since you already have a DateTime, you could just format it:

string s = last_backup_date.ToString("dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);

There is no need to first convert it to a string and then try to convert it back to a DateTime. A DateTime object itself has no specific format by the way.

Sign up to request clarification or add additional context in comments.

6 Comments

yeah, but that leaves my datetime as a string and thus i can't order it on my datagrid
What do you mean? How would convert it to and back from a string make any difference?
@MatthewAlmeida - than you need to apply the format of the datetime object on your datagridcolumn in winforms eg. stackoverflow.com/questions/4033113/…
You could format it in the DataGrid only if that's what you want.
@mm8 Straight forward, beat me to it. Good answer.
|
0

Can you try this?

Var last_backup_date_string = last_backup_date.ToString("dd/MM/yyyy HH:MM:ss", CultureInfo.InvariantCulture)

2 Comments

the string formatted, but the datetime continues the same
@MatthewAlmeida: A DateTime object has no specific format...
0

Based on the information you have, the conversion should not be an issue. However, the problem area is probably in your ParseExact. Your current date format starts with month not the day. You should be able to do the format to display in your preferred manner.

Console.WriteLine($"{date.ToString("dd/MM/yyyy")}");
Console.WriteLine($"{date.ToString("MMMM dd, yyyy")}");
Console.WriteLine($"{date.ToString("MM-dd-yyyy")}");

Otherwise you could use the DateTime.Parse method or DateTime.TryParse so you do not have to specify the culture variation.

Example:

DateTime date;
var current = DateTime.Now.ToString();
if(DateTime.TryParse(current, out date))
     Console.WriteLine(date);

DateTime date;
var current = "6/22/2020 10:03:40 AM";
if(DateTime.TryParse(current, out date))
     Console.WriteLine($"{date:dd/MM/yyyy}");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.