i need to format date like "2010-04-21 11:35:22.440". can anyone help me?
the problem is i am seeing either 2009-06-15T13:45:30.0900000 or 2008-03-09 16:05:07Z but not the one i am looking for. thanks.
string formattedDate = dateTime.ToString("yyyy-MM-dd HH\\:mm\\:ss.fff");
Note that the case of MM and HH are important, MM is months, mm is minutes, and HH is 24h, vs hh being 12h.
Also notice the time separator is specified as \:, if you just use : it will use the time separator specified in your regional settings, which may not necessarily be a colon.
Custom Date and Time Format Strings link should point you in the right direction.
Use the format "yyyy-MM-dd hh:mm:ss.fff" with the ToString of the date variable.
e.g:
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
Related
I am trying to parse the date by using below code
DateTime mydate = DateTime.ParseExact(datetoconvert,"dd/mm/yyyy",System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat);
but its output is wrong, the datetoconvert in above code is 30/Mar/2017 but output is 29/Jan/2017
looking forward for your valuable answers...
Lowercase mm means minute, use MM
DateTime mydate = DateTime.ParseExact(datetoconvert,"dd/MM/yyyy",System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat);
If you want to output it as 30/Mar/2017(different topic):
string result = mydate.ToString("dd/MMM/yyyy", CultureInfo.InvariantCulture);
But note that / has a special meaning too(in Parse and ToString). It will be replaced with your current cultures date-separator which seems to be / but fails with a different. You can avoid it by specifying CultureInfo.InvariantCulture or by masking it by wrapping it with apostrophes:
DateTime mydate = DateTime.ParseExact(datetoconvert,"dd'/'MM'/'yyyy",System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat);
replace
"dd/mm/yyyy"
with
"dd/MMM/yyyy"
because "Jan" is matched by MMM instead of mm (for minutes)
Reference
"MMM" The abbreviated name of the month.
https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
The date format is wrong. try "dd/MM/yyyy" instead of "dd/mm/yyyy"
If you need abbrivated month name, use "dd/MMM/yyyy"
I would like to know if there is a way to get the current date in the following format :
YYYY.MM.DD
I know that I can get the current date, and then manipulate it to get the desired format, but I was wondering if there is a pattern solution to specify my own format and so directly getting a string like this :
"2014.06.10"
string dateFormatted = DateTime.Today.ToString("yyyy.MM.dd");
You can use the string.Format() method and reference this link http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
The {#} represents the argument being passed and the {#:yyyy} is a way of formatting to grab just the year in format of yyyy. Same for MM and dd. Worth noting is that mm is minutes and MM is months which may be confusing as day and year are also lower case.
string.Format("{0:yyyy}.{0:MM}.{0:dd}", DateTime.Now);
I tried converting 9/29/2013 2:44:28 PM (mm/dd/yyyy) to dd/mm/yyyy format.
I got a strange Date after Converting.
I tried
dateTimeVar.ToString("dd/mm/yyyy");
29/44/2013
The Date was a type of DateTime itself.
Lowercase mm means minutes, try this instead:
dateTimeVar.ToString("dd/MM/yyyy");
However, if this works depends on your local culture. If your current culture's date separator is different, / will be replaced with that. So if you want to enforce it use CultureInfo.InvariantCulture:
dateTimeVar.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
MM is for months, mm is for minutes. That's why it gets your minutes (which is 44) instead of your month value.
Use it like;
dateTimeVar.ToString("dd/MM/yyyy");
Check out;
The "MM" Custom Format Specifier
The "mm" Custom Format Specifier
And remember, / has special meaning when you use it as a date separator. It replace itself with your current culture date separator. Forcing to use with InvariantCulture would be better.
dateTimeVar.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
Take a look at;
The "/" Custom Format Specifier
What if I want to convert a string in dd/MM/yyyy to DateTime?
Then you can use DateTime.ParseExact method.
Converts the specified string representation of a date and time to its
DateTime equivalent using the specified format and culture-specific
format information. The format of the string representation must match
the specified format exactly.
As an example;
string s = "01/01/2013";
DateTime dt = DateTime.ParseExact(s, "dd/MM/yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(dt);
Output will be;
1/1/2013 12:00:00 AM
Here a DEMO.
dateTimeVar.ToString("dd/mm/yyyy"); // Change to dd/MM/yyyy
The problem is mm stands for minute and you need MM which would be months
Tim's answer is correct, but to remove the format string altogether you can use. 'ToShortDateString'
DateTime date = DateTime.Today;
var stringDate = date.ToShortDateString();
var stringDate2 = date.ToString("dd/MM/yyyy");
I'm trying to convert a date in yyyymmdd format to yyyy-mm-dd with the following code:
tdrDate = DateTime.ParseExact(dateString, "yyyymmdd", null).ToString("yyyy-MM-dd");
This works the only problem is that when I have a date such as this "20070205" I get back "2007-01-05". I don't know why this is happening, any help is appreciated.
tdrDate = DateTime.ParseExact(dateString, "yyyyMMdd", null).ToString("yyyy-MM-dd");
You need MM, not mm. mm is for minutes.
It should be:
DateTime.ParseExact(dateString, "yyyyMMdd", null).ToString("yyyy-MM-dd");
Capital 'MM' in the first date format string.
"yyyymmdd" must be "yyyyMMdd".
mm is for minutes.
Try this :
tdrDate = DateTime.ParseExact(dateString, "yyyyMMdd", null).ToString("yyyy-MM-dd");
use MM instead mm,
mm is for minute &
MM is for Month that is why it is taking 01 (default value of MM).
The format string is case-sensitive, so "mm" is different to "MM". You are parsing minutes ("mm"), which is why the value of months ("MM") is always at the default value of 1.
try this:
DateTime.ParseExact("20070205", "yyyyMMdd", null).ToString("yyyy-MM-dd")
Disclaimer I know nothing about C#'s date formatting.
But I'm guessing the problem is that you used mm in the first format string, and MM in the second.
A handy reference: SteveX Compiled: String formatting in C#
Here you're parsing the date to create a date object, formatting the date object to a string, and discarding the date object. That sounds like more work than simple string processing:
tdrDate = dateString.Substring(0,4) + '-' +
dateString.Substring(4,2) + '-' +
dateString.Substring(6,2);
Unless you need the validation that's performed by DateTime.ParseExact(), which will throw a System.FormatException if given an invalid date, I'd probably just use the string formatting approach.
I was searching here about converting a string like "16:20" to a DateTime type without losing the format, I said I dont want to add dd/MM/yyy or seconds or AM/PM, because db just accept this format.
I tried with Cultures yet
Thanks in Advance
Just give a date format to your dateTime.
string DateFormat = "yyyy MM d " this willl give you the year month and day. after continuing;
string DateFormat = "yyyy MM d HH:mm:ss " in here the Capital H will give you the 24 hours time format and lowerCase "h" will give you the 12 hours time format...
when you give the Dateformat as a string you can do whatever you want with date and time.
string DateFormat = "yyyyMMdHHmmss";
string date = DateTime.Now.ToStrign(DateFormat);
OR
Console.writeline(DateTime.Now.ToStrign(DateFormat));
OUTPUT:
20120823132544
All DateTime objects must have a date and a time.
If you want just the time, use TimeSpan:
TimeSpan span = TimeSpan.Parse("16:20");
If you want a DateTime, add that time to the min value:
TimeSpan span = TimeSpan.Parse("16.20");
DateTime dt = DateTime.MinValue.Add(span);
// will get you 1/1/1900 4:20 PM which can be formatted with .ToString("HH:mm") for 24 hour formatting
DateTime.Now.ToString("hh:mm") - If it's C#.
Oh. Only read the header.
DateTime dt = new DateTime(2008, 12, 11, Convert.ToInt32("16"), Convert.ToInt32("32"), 0);
what do you mean by "losing the format".
if you convert it to a DateTime type, then the DateTime object will have dd/mm/yy and other properties. depending on how you plan to use the object, you can "recover" your original settings, by formatting the string output like this: DT.ToString("HH:mm");
Since you don't stipulate which DBMS you are using, it is hard to know which answer will help you. If you use IBM Informix Dynamic Server, you would simply use the data type 'DATETIME HOUR TO MINUTE', which will record values in the 24 hour clock.
DateTime.Parse("16:20")
I want to address this part of your question:
without losing the format
A database will generally store all datetime values in a standard common format that's not even human readable. If you use a datetime column the original format is destroyed.
However, when you retrieve the value you cast it back to any format you want. If you want HH:mm you can get it.