-7

This code is a simplified version of what I'm trying to do:

this.dateValue = "8/12/2005";

return DateTime.dateValue.DayOfWeek.ToString();

I want to use my dateValue in stead of 'NOW'

Mike G
  • 4,232
  • 9
  • 40
  • 66
Awa
  • 160
  • 1
  • 3
  • 11
  • I'm not entirely sure what you're asking, this won't even compile as written. – Mike G Jan 13 '15 at 16:34
  • I think this made more sense before the edit... – Liath Jan 13 '15 at 16:35
  • 1
    (The duplicate doesn't have the same format, but you should be able to figure out what to do from it.) – Jon Skeet Jan 13 '15 at 16:36
  • @Liath you mean the edit where I removed the snippet? Because this isn't web code and it's not JavaScript? – Mike G Jan 13 '15 at 16:36
  • I want to convert "8/12/2005" To a day of the week – Awa 10 secs ago edit – Awa Jan 13 '15 at 16:36
  • @mikeTheLiar I think the original was a bit misleading... hence why I originally got my question the wrong way around. You're right - good edit and dup – Liath Jan 13 '15 at 16:45

2 Answers2

4

Use DateTime.TryParseExact

this.dateValue = "8/12/2005";
DateTime dt;
// assuming the expected date is the 8th of Dec 2005, otherwise use m/d/yyyy
if (DateTime.TryParseExact(this.dateValue, "d/m/yyyy", CulterInfo.InvariantCulture, 
                       DateTimeStyles.None, out dt)) 
{
    return dt.DayOfWeek.ToString();
} 
else 
{
    return null;
}
James
  • 80,725
  • 18
  • 167
  • 237
-1

The DateTime.Now property returns a DateTime, DayOfWeek is a property of DateTime so you can simply do

return DateTime.Now.DayOfWeek.ToString();

To get the text version of the current Day of the Week

If you want to reverse this you can parse a new DateTime from the string.

return DateTime.Parse("8/12/2005").DayOfWeek.ToString();

Watch our for strings which can't be parsed. You may want to look into the TryParse method.

Liath
  • 9,913
  • 9
  • 51
  • 81
  • 2
    He's asking how to use his date *instead* of `Now`, not how to use `Now` instead of his date. – Servy Jan 13 '15 at 16:35
  • I want to convert "8/12/2005" To a day of the week – Awa Jan 13 '15 at 16:36
  • @Servy, thanks that wasn't my impression from the original question. I've edited to include both. – Liath Jan 13 '15 at 16:36
  • 1
    It's recommended you use, at the very least, `ParseExact` when trying to parse a date of a particular format. `Parse` assumes the expected format from the current system environment which may not always match up with the input format. – James Jan 13 '15 at 16:39