C # - Is there a string representation of DateTime.Now ()?

I need to parse the actual date value from the configuration file. So I need to know if there is any string representation DateTime.Now()?. So can I make it out through DateTime.Parse(..)?

thank

EDIT

Sorry, I didn’t explain myself very well. I will explain my question again. I have a configuration 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 indicated in the Value attribute!

+3
source share
7 answers

, , , . , , , , DateTime.Now, , DateTime.Now . , , , Now:

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);
}
+6

DateTime.Parse :

  • .
  • , .
  • , .
  • , ISO 8601. , (UTC); , UTC:
  • 2008-11-01T19: 35: 00.0000000Z
  • 2008-11-01T19: 35: 00.0000000-07: 00
  • , GMT ​​ RFC 1123. :
    • , 01 2008 19:35:00 GMT
  • , , . :
    • 03/01/2009 05:42:00 -5: 00

, DateTime.ParseExact.

, DateTime.Now.ToString().

+3

, , DateTime.Parse(...)

- , ; :

DateTime today = DateTime.Parse("12:34"); // Currently returns 2010/06/04 12:34
+3
DateTime.Now.ToLongDateString();
DateTime.Now.ToLongTimeString();
DateTime.Now.ToShortDateString();
DateTime.Now.ToShortTimeString();
DateTime.Now.ToString("insert a valid date formatter string here");

, , :

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

ToString() Now , .

"04/06/2010 11:41:43"

+1

, , /. , , ASP.NET:

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

, GetConfigParam ( "StartTime" ), :

<!-- 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}" />

({0} String.Format ):

string startTimeParam = GetConfigParam("StartTime");
string startTime = String.Format(startTimeParam, DateTime.Now);
+1

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

If this is what you want, I think the closest thing you can do is to keep an access log that keeps a record for each request, but that, of course, depends on the reason why you need dates in the first place.

0
source

Source: https://habr.com/ru/post/1748503/


All Articles