Take the 2-minute tour ×

I need to parse the actual date value from a config file. So I need to know if there is any string representation of DateTime.Now()? So I can parse it via DateTime.Parse(..)?



Sorry I didn't explain my self pretty well. I reexplain my question. I have my config file that has the following section:

  <Parameter Name="StartTime" Value="**I Want to put a string here that will be parsed as DateTime.Now()**"/>

Pay attention to what is specified in the Value attribute!

share|improve this question
Dates are formatted according to locale settings. Can you be more specific? –  Philippe Leybaert Jun 4 '10 at 10:38
Please elaborate on what your are trying to do. You can of course get a string representation by using DateTime.Now.ToString(), but something tells me thats not what you are asking. –  Jens Jun 4 '10 at 10:38
If I understand correctly, you want a string that will parse always into the current date at time of parsing? –  Ilia Jerebtsov Jun 4 '10 at 10:42
@Ilia : Exactly –  Amokrane Chentir Jun 4 '10 at 10:43
Why not have special string, e.g. "<today>", that you check for before you try to parse the string using DateTime.Parse(..)? –  Peter Jun 4 '10 at 10:47
show 4 more comments

7 Answers

up vote 6 down vote accepted

I don't think you can achieve what you want by simply parsing a datetime. I think what you are trying to do, is have a value that represents DateTime.Now, none exist, because DateTime.Now is forever changing. What you may want to do is allow a special string which you want to represent the Now value:

public DateTime GetDateTime(string value)
  if (string.IsNullOrEmpty(value))
    return DateTime.MinValue;

  if ("{now}".Equals(value, StringComparison.InvariantCultureIgnoreCase))
    return DateTime.Now;

  return DateTime.Parse(value);
share|improve this answer
add comment

DateTime.Parse will parse a number of formats:

  • A string with a date and a time component.
  • A string with a date but no time component.
  • A string with a time but no date component.
  • A string that includes time zone information and conforms to ISO 8601. For example, the first of the following two strings designates the Coordinated Universal Time (UTC); the second designates the time in a time zone seven hours earlier than UTC:
  • 2008-11-01T19:35:00.0000000Z
  • 2008-11-01T19:35:00.0000000-07:00
  • A string that includes the GMT designator and conforms to the RFC 1123 time format. For example:
    1. Sat, 01 Nov 2008 19:35:00 GMT
  • A string that includes the date and time along with time zone offset information. For example:
    1. 03/01/2009 05:42:00 -5:00

If you want further control over the parsing you can use DateTime.ParseExact.

With regards to your edit, just use DateTime.Now.ToString().

share|improve this answer
Hmm so according to you I can't do what I want :/ –  Amokrane Chentir Jun 4 '10 at 10:43
No that's not I want! I can't do: DateTime.Parse("DateTime.Now.ToString()"); you see? –  Amokrane Chentir Jun 4 '10 at 10:47
It's still not clear what you wanted. Do you want a string that always represents the time 'now'? Because that isn't possible. It must be calculated, not read from a config file. Config files hold static values, if you want it to hold a 'special' value that tells the code to calculate the time then you'll need to define it yourself and handle it so that the code knows to use DateTime.Now. –  David Neale Jun 4 '10 at 10:48
add comment
DateTime.Now.ToString("insert a valid date formatter string here");

BTW, if I've understand your need correctly, the right solution would be:

parse the date from the text file with DateTime.TryParse();
now you have a DateTime variable with the text file date stored.
now you can do
DateTime textParsedDate = new DateTime(2010, 6, 4); //your textFile parsed date.
int result = DateTime.Now.CompareTo(textParsedDate); //refer to the documentation to understand the result of DateTime.CompareTo
share|improve this answer
add comment
var dateAsString = DateTime.Now.ToString();

The ToString() of the Now property produces what you're after.

Its format is "04/06/2010 11:41:43"

share|improve this answer
add comment

It seems to me that you are getting a string from a configuration file and that you expect it to contain the current date/time. You expect it to behave a bit like what people might've gotten used to in ASP.NET:

<p>Current date: <%= DateTime.Now.ToString() %>.</p>

To do the same from any configuration file, assuming your function is called GetConfigParam("StartTime"), you can do the following:

<!-- inside config file -->
   Value="Current date/time: {0}"/>

<!-- or, if you just want the current date/time (but that gets a bit silly
     as you can do that by code as has been suggested here before -->
<Parameter Name="StartTime" Value="{0}" />

Then, you use this code for parsing the above (the {0} will be filled by String.Format and can be used with any object):

string startTimeParam = GetConfigParam("StartTime");
string startTime = String.Format(startTimeParam, DateTime.Now);
share|improve this answer
add comment

As others have suggested, the best solution would be to add code to check for a magic value before running your value through DateTime.Parse(...)

If this is not possible for some reason, the next best solution is to specify only the time; the current day's date will be returned with the time you specify:

DateTime today = DateTime.Parse("12:34"); // Currently returns 2010/06/04 12:34
share|improve this answer
add comment

Do you want to have the text in the Value attribute be something like an auto-updating field?

If this is what you want, I think the closest thing you can do is hold an access log that saves an entry for each request, but that of course depends on the reason why you want the dates in the first place.

share|improve this answer
add comment

Your Answer


