tags:

views:

154

answers:

7

Hi,

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(..)?

Thanks

EDIT

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

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

Pay attention to what is specified in the Value attribute!

+2  A: 

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().

David Neale
Hmm so according to you I can't do what I want :/
Amokrane
No that's not I want! I can't do: DateTime.Parse("DateTime.Now.ToString()"); you see?
Amokrane
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
+1  A: 
DateTime.Now.ToLongDateString();
DateTime.Now.ToLongTimeString();
DateTime.Now.ToShortDateString();
DateTime.Now.ToShortTimeString();
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
vaitrafra
+5  A: 

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);
}
Matthew Abbott
+1  A: 
var dateAsString = DateTime.Now.ToString();
Console.WriteLine(DateTime.Parse(dateAsString));

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

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

Antony Koch
A: 

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.

Rox
+1  A: 

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 -->
<Parameter 
   Name="StartTime" 
   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);
Abel
+1  A: 

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
Zelphar