C# format date
-
In a C# 2008 application, I will obtain the current date by using the statement:
DateTime.Now;
I want to read from a file that is in part of the directory path of: mm_dd_yy. Or in other words the format is: 09_27_12. Thus can you tell me how to change the date obtained from datetime.now and make it look like: 09_27_12?
-
In a C# 2008 application, I will obtain the current date by using the statement:
DateTime.Now;
I want to read from a file that is in part of the directory path of: mm_dd_yy. Or in other words the format is: 09_27_12. Thus can you tell me how to change the date obtained from datetime.now and make it look like: 09_27_12?
-
In a C# 2008 application, I will obtain the current date by using the statement:
DateTime.Now;
I want to read from a file that is in part of the directory path of: mm_dd_yy. Or in other words the format is: 09_27_12. Thus can you tell me how to change the date obtained from datetime.now and make it look like: 09_27_12?
-
In a C# 2008 application, I will obtain the current date by using the statement:
DateTime.Now;
I want to read from a file that is in part of the directory path of: mm_dd_yy. Or in other words the format is: 09_27_12. Thus can you tell me how to change the date obtained from datetime.now and make it look like: 09_27_12?
Ravi has provided you with a link that should answer your question, so I'm not going to add to it. I'd like to make a suggestion as well. If you can, change the format of the date: yyyy-MM-dd yyyy_MM_dd or yyyymmdd or somesuch The format you have can be ambiguous between US and European date formats. This might not be a problem in itself (you may only ever deal in US dates). The other thing changing the date will do is allow you to sort the files by date via their filenames.
Sort of a cross between Lawrence of Arabia and Dilbert.[^]
-Or-
A Dead ringer for Kate Winslett[^] -
In a C# 2008 application, I will obtain the current date by using the statement:
DateTime.Now;
I want to read from a file that is in part of the directory path of: mm_dd_yy. Or in other words the format is: 09_27_12. Thus can you tell me how to change the date obtained from datetime.now and make it look like: 09_27_12?
You would use a DateFormat string of
MM_dd_yy
, which can either be passed to the ToString method on a DateTime:var formatted = DateTime.Now.ToString("MM_dd_yy")
or used in a larger format string, say for example if you're building up a path:
var path = String.Format("Some/Directory_{0:MM_dd_yy}/somefile.txt",DateTime.Now);
Here's a live example of the latter: http://rextester.com/UTHQ72076[^]
-
string date = DateTime.Now.ToString("dd/MM/yyyy");
string time = DateTime.Now.ToString("hh:mm:ss"); -
In a C# 2008 application, I will obtain the current date by using the statement:
DateTime.Now;
I want to read from a file that is in part of the directory path of: mm_dd_yy. Or in other words the format is: 09_27_12. Thus can you tell me how to change the date obtained from datetime.now and make it look like: 09_27_12?
Try this.
protected string FormatDate()
{
string strTemp;
strTemp = DateTime.Now.ToString("MM/dd/yy").Replace("/", "_");
return strTemp;
}KiranKumar Roy