I am trying to convert a datatime string "including milliseconds" into a DataTime. I tried tried to use DateTime.TryParseExact but it does not give me a milliseconds.
Here is what I have tired
public static DateTime? dateTimeVal(string raw, string format = null)
{
DateTime final;
if(raw != null){
if( format != null){
string[] formats = new[] { format };
if (DateTime.TryParseExact(raw, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out final))
{
return final;
}
}
if (DateTime.TryParse(raw, out final))
{
return final;
}
}
return null;
}
This is how I use the method above
DateTime? dt = dateTimeVal("2016-03-14 11:22:21.352", "yyyy-MM-dd HH:mm:ss.fff");
However, dt gives me this 3/14/2016 11:22:21 AM
How can I get the value to include the milliseconds?
DateTime final = new DateTime();
var test = DateTime.TryParseExact("2016-03-14 11:22:21.352", new string[] { "yyyy-MM-dd HH:mm:ss.fff" }, CultureInfo.InvariantCulture, DateTimeStyles.None, out final);
This works for me.
Just take care for your fff. If you write them uppercase, the milliseconds will be supressed. Lowercase they will be parsed.
The only thing away from that I can imagine is you have used .ToString without providing any format. Then you'll get:
Are you sure you've written the providing format lowercase inside your code? Also have you used .ToString() with an output-format that shows up the milliseconds?
If you want to get the DateTime as string with milisecond, then you can use the following code;
string dateTime = dt.Value.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
Hope this helps.
Related
I'm trying to convert a Date into a specific format, I saw a lot of questions here with the same target but all the proposed solutions return a string, I need to return a DateTime in my custom format.
This is my code:
private DateTime? _matchCalendarDate = DateTime.Now;
public DateTime? MatchCalendarDate
{
get
{
var date = _matchCalendarDate.Value.ToString("dd-MM-yyyy");
var c = DateTime.ParseExact(date, "dd-MM-yyyy", CultureInfo.InvariantCulture);
return c;
}
set
{
_matchCalendarDate = value;
OnPropertyChanged();
}
}
this return: 8/15/2018 12:00:00 AM but should return 15/08/2018
When you say it returns 8/15/2018 12:00:00 AM, I'm guessing you're simply calling ToString() on the property, like so:
MatchCalendarDate.ToString();
The thing is, a DateTime object doesn't have it's own inherent 'format'. It's format is whatever you want it to be.
So when you actually use the property to print the value it returns, you can choose how you want it do be displayed.
Something like;
MatchCalendarDate.ToString("dd-MM-yyyy");
But then, that essentially renders the conversion in your property redundant. So assuming your intention is to store a DateTime object, but retrieve it in the format you like, what you should do is declare a second string property that does the conversion for you.
Return matchCalendarDate.Date; returns the date component, time set to zero
You may have to consider converting to the original format first then to your required format
private DateTimeDateTime _matchCalendarDate, _matchCalendarD = DateTime.Now;
public DateTime MatchCalendarDate
{
get
{
var date = _matchCalendarDate.Value.ToString("dd-MM-yyyy");
var dt = DateTime.ParseExact(date, "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
var c = dt.ToString("dd/M/yyyy", CultureInfo.InvariantCulture);
return c;
}
set
{
_matchCalendarDate = value;
OnPropertyChanged();
}
}
I am facing an issue in the parsing of the date. I have made following common function.
public static string ConvertedDate(string date)
{
if(!string.IsNullOrEmpty(date))
{
DateTime returnValue;
bool flag = DateTime.TryParseExact(date, "yyyy-MM-dd hh:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out returnValue);
return flag ? returnValue.ToString("MM.dd.yyyy") : null;
}
else
{
return null;
}
}
But very strange thing happens some strings are successfully converted into DateTime but some return false.
For example :
"2017-05-11 12:00:24" this string parse successfully.
"2015-03-06 20:18:42" this string can not.
The format of the string is same.
I observed that when "hour(hh)" goes above 12 then it cannot be parsed.
You need to change yyyy-MM-dd hh:mm:ss to yyyy-MM-dd HH:mm:ss, which is hours in 24 hour time. Note the change from hh to HH.
See here: C# DateTime to "YYYYMMDDHHMMSS" format
My system time is of the format dd-MMM-yy (02-Dec-16). The format I want to convert it to is "yyyy/MM/dd". I've basically been playing around with all the other datetime formats that my system offers and this is the parsing statement I've figured out that works for All of them (except this) -
CultureInfo provider = CultureInfo.InvariantCulture;
string date_format = "yyyy/MM/dd HH:mm:ss tt";
DateTime now_value = DateTime.ParseExact(DateTime.Now.ToString(date_format), date_format, provider);
return now_value.ToString(date_format);
But this doesn't work for the aforementioned dd-MMM-yy format. Can someone please tell me what I am doing wrong here?
(Sidebar -Is there a more efficient way in which I can write this above snippet?)
You don't need to convert DateTime to string and then convert back to DateTime and again back to string, if you have DateTime input just call the ToString with the format as below
string dt =DateTime.Now.ToString("yyyy/MMM/dd", CultureInfo.InvariantCulture);
for your example :
DateTime now_value = DateTime.ParseExact("02-Dec-16", "dd-MMM-yy", System.Globalization.CultureInfo.InvariantCulture);
return now_value.ToString("yyyy/MM/dd");
Try This:
string date_format = "yyyy-MMM-dd";
string date_now = DateTime.Now.ToString(date_format,CultureInfo.CreateSpecificCulture("en-US"));
return date_now;
Even This should also work:
string date_format = "yyyy-MMM-dd";
string date_now = DateTime.Now.ToString(date_format);
return date_now;
I think best way would be to create an extension method for multiple date formats,
var inputDate = "02-Dec-2016";
string[] availaible_input_date_format = { "dd-MMM-yyyy", "dd/MMM/yyyy" }; // add as many formats availible
var date_format = "yyyy/MMM/dd";
DateTime outputDate;
DateTime.TryParseExact(inputDate, availaible_input_date_format, null, DateTimeStyles.None, out outputDate);
Console.WriteLine(outputDate.ToString(date_format));
You can try this:
datetime yourdatetime = new datetime();
string converteddatetime = yourdatetime.toString("yyyy/MM/dd");
I am trying to convert a string, 20151107 to the date format of 2015-11-07.
Here's my code :
public static DateTime CustomDateFormat(this string resultdate)
{
DateTime dt = DateTime.ParseExact(resultdate, "yyyyMMdd", CultureInfo.InvariantCulture);
return dt;
}
However this returns something like this 11/07/2015 12:00:00 AM.
Any idea?
Your date returns like that because you are returning the entire DateTime object and since you are not providing a time it is default to 00:00:00.00.
If you want to return the Date in a particular format, you can use the Standard Format Strings or a Custom Format String.
In your case, you want 2015-11-07 which is a custom format of yyyy-MM-dd and can be used like so:
public static string CustomDateFormat(string resultdate)
{
DateTime dt = DateTime.ParseExact(resultdate, "yyyyMMdd", CultureInfo.InvariantCulture);
return dt.ToString("yyyy-MM-dd");
}
public static String CustomDateFormat(this string resultdate)
{
DateTime dt = DateTime.ParseExact(resultdate, "yyyyMMdd", CultureInfo.InvariantCulture);
return dt.ToString("yyyy-MMM-dd");
}
I'm working on formatting datetime's to display on a graph, and its working great so far. I have the format string:
M/dd/yyyy HH:mm:ss
and it prints as I'd like it to except the following: I want to completely hide the HH:mm:ss if the time is midnight.
Is it possible to do without a custom iformatprovider?
Thanks!
DateTime time = DateTime.Now;
string txt = time.ToString(time == time.Date ? "M/dd/yyyy" : "M/dd/yyyy HH:mm:ss");
DateTime time = DateTime.Now;
string format = "";
if (time.Hour == 0)
{
format = "M/dd/yyyy";
}
else
{
format = "M/dd/yyyy HH:mm:ss";
}
A culture-independent version of the solution proposed by #JDunkerley would be:
DateTime time = DateTime.Now;
string txt = time.ToString(time == time.Date ? "d" : "g");
More about standard date and time format strings here.
The proposed solution is not a very good one because it does not take into account user's localized time format, and simply assumes US English.
Here's how it should've been done:
public static string formatDateTimeWithTimeIfNotMidnight(DateTime dt)
{
//RETURN:
// = String for 'dt' that will have time only if it's not midnight
if (dt != dt.Date)
{
//Use time
return dt.ToString();
}
else
{
//Only date
return dt.ToShortDateString();
}
}