Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I make a datetime into a specific custom format?

Tags:

c#

.net

datetime

Say the current date is 1st Mar 2010, I want to display it like this...

20100301 so like first 4 digits = year, 2 digits = Month, 2 digits = day

is there an easy way to do this?

like image 393
Diskdrive Avatar asked Jun 17 '10 05:06

Diskdrive


People also ask

How do I convert a datetime to a specific format?

The ToString() method of the DateTime class is used to convert a DateTime date object to string format. The method takes a date format string that specifies the required string representation.

How do I create a custom date format?

Create a custom date formatPress CTRL+1. In the Format Cells box, click the Number tab. In the Category list, click Date, and then choose a date format you want in Type. You can adjust this format in the last step below.

How can I change the date format of a datetime?

Use datetime. strftime(format) to convert a datetime object into a string as per the corresponding format . The format codes are standard directives for mentioning in which format you want to represent datetime. For example, the %d-%m-%Y %H:%M:%S codes convert date to dd-mm-yyyy hh:mm:ss format.

How do I create a custom time format in Excel?

Create a custom date or time formatOn the Home tab, click the Dialog Box Launcher next to Number. You can also press CTRL+1 to open the Format Cells dialog box. In the Category box, click Date or Time, and then choose the number format that is closest in style to the one you want to create.


1 Answers

use format

yourdatetimeObj.ToString("yyyyMMdd");

Ref: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

like image 97
Luke Schafer Avatar answered Oct 03 '22 14:10

Luke Schafer