C# DateTime to YYYYMMDDHHMMSS format
DateTime.Now.ToString("yyyyMMddHHmmss"); // case sensitive
Format date in C#
from http://www.csharp-examples.net/string-format-datetime/
// create date time 2008-03-09 16:05:07.123
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);
String.Format("{0:y yy yyy yyyy}", dt); // "8 08 008 2008" year
String.Format("{0:M MM MMM MMMM}", dt); // "3 03 Mar March" month
String.Format("{0:d dd ddd dddd}", dt); // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}", dt); // "4 04 16 16" hour 12/24
String.Format("{0:m mm}", dt); // "5 05" minute
String.Format("{0:s ss}", dt); // "7 07" second
String.Format("{0:f ff fff ffff}", dt); // "1 12 123 1230" sec.fraction
String.Format("{0:F FF FFF FFFF}", dt); // "1 12 123 123" without zeroes
String.Format("{0:t tt}", dt); // "P PM" A.M. or P.M.
String.Format("{0:z zz zzz}", dt); // "-6 -06 -06:00" time zone
See also:
Custom Date and Time Format Strings - MSDN
if you only have the string, just split the string to an array, and concatenate the parts you want in another order
String str = "Mon, dd Dec YYYY hh:mm:ss";
String[] strArr = str.Split(" ");
str = strArr[2] + " " + strArr[3];
If the date can change, then do what SLaks posted in his answer
DateTime string format in c#
If I understand correctly, you can use DateTime.Today
property like;
var dt1 = DateTime.Today;
var dt2 = DateTime.Today.AddDays(1).AddSeconds(-1);
and use DateTime.ToString()
to format them like;
var DateFormatFrom = dt1.ToString("MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
var FilloutDateTo = dt2.ToString("MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
Results will be;
12/04/2014 00:00:00
12/04/2014 23:59:59
You used hh
format specifier but it is for 12-hour clock. Use HH
format specifier instead which is for 24-hour clock. And since your result strings doesn't have any AM/PM designator, you don't need to use tt
format specifier.
Format date using variable
Just use DateTime.ToString
:
string val = dd.ToString( dateFormat );
You are confusing String.Format
with your format string which does work only in this way {0:yyyyMMdd}
.
C# Date Time Formatting
Use a custom DateTime
formatting string:
// Returns Jan 31, 2012
myDateTimeObject.ToString("MMM dd, yyyy");
// Returns 31 January, 2012
myDateTimeObject.ToString("dd MMMM, yyyy");
All of the custom date/time formats are listed here.
C# Date Format vs. Windows Date Format
In a date format string, "/"
is not a literal character, but instead represents the locale's "date separator".
Apparently, in your locale, the date separator is a space.
Per the linked documentation, you can escape it with single quotes to get a literal slash:
string dateString= String.Format("{0:MM'/'dd'/'yyyy}",((DateTime)reader[columnName]));
(its causing a number of things to break that rely on formatting).
That's bad anyway — you should separate form and function. With that fixed, you can happily display dates to your users in the form with which they are most familiar, and in the form they would expect.
DateTime format to SQL format using C#
try this below
DateTime myDateTime = DateTime.Now;
string sqlFormattedDate = myDateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
Related Topics
How to Compile and Execute New Code at Runtime in .Net
Deserialize Collection of Interface-Instances
Write a Well Designed Async/Non-Async API
When Is a Static Constructor Called in C#
Two Different Dll with Same Namespace
Regular Expression to Remove HTML Tags
Transparent Background Image for Form - Smooth Edge Shape for the Form
How to Read and Modify Ntfs Alternate Data Streams Using .Net
How to Decode a Url Parameter Using C#
Optimal Linq Query to Get a Random Sub Collection - Shuffle
Changing the User Agent of the Webbrowser Control
Why Can't I Have Abstract Static Methods in C#
Excel Interop - Efficiency and Performance
Has an Event Handler Already Been Added
How to Read Data of an Excel File Using C#
When Should "Sqldbtype" and "Size" Be Used When Adding SQLcommand Parameters