I have an object "2/17/2011 6:46:01 PM".I want to convert this object to 6:46 PM
string myDateString = "2/17/2011 6:46:01 PM";
DateTime datetime = DateTime.Parse(myDateString);
string timeString = datetime.ToShortTimeString();
Console.WriteLine(timeString); // 6:46 PM
You can format the parsed datetime to a string in many other ways, but the ToShortTimeString does exactly what you want.
You can format the object as
strdate = convert.todatetime(object);
strdate .tostring("hh:mm tt");
or
strdate.toshorttime();
May be you need just format?!
DateTime.Parse(obj.ToString()).ToString("h:mm tt");
using System.Globalization;
...
string dateString, format;
format = "M/dd/yyyy h:mm:ss tt";
dateString = "2/17/2011 6:46:01 PM";
DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;
result = DateTime.ParseExact(dateString, format, provider);
Console.WriteLine(result.ToString());
See here for more info:
http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx
If input is a string in first convert it to a dateTime by DateTime.parse method and then convet it to shortTimeString Or other
If input is DateTime convert it to shorttimeString in this form : input.toShortTimeString
Related
I read date-time strings from a file in 2 different formats:
19/02/2019 08:24:59
2/17/2019 12:25:46 PM
For the first format the custom format string I wrote is:
string firstDate = "19/02/2019 08:24:59";
string customFormatForFirstDateTimeString = "dd/mm/yyyy hh:mm:ss";
and I use it as follows:
string firstResultingDateAndTime;
bool parsingSuccessful = DateTime.TryParseExact(
firstDate,
customFormatForFirstDateTimeString,
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out firstResultingDateAndTime);
The problem is that parsingSuccessful results false.
For the second date-time string, the code is as follows:
string secondDate = "2/17/2019 12:25:46 PM";
string customFormatForSecondDateTimeString = "m/dd/yyy hh:mm:ss PM";
string secondResultingDateAndTime;
parsingSuccessful = DateTime.TryParseExact(
secondDate,
customFormatForSecondDateTimeString,
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out secondResultingDateAndTime);
Also here I receive
parsingSuccessful == false;
I reckon that the custom format strings do not fit the date-time strings, but I was not able to figure out why.
Please help.
Thank you in advance.
Well, mm stands for minutes, not months (we have MM for it) that's why dd/mm/yyyy format should be dd/MM/yyyy.
Another issue with hour format where we have hh for 0..12 range (with tt for AM/PM) and HH for 0..23 interval:
string firstDate = "19/02/2019 08:24:59";
// Since we don't have AM / PM we can conclude that hour is in 0..23 range
string customFormatForFirstDateTimeString = "dd/MM/yyyy HH:mm:ss";
string secondDate = "2/17/2019 12:25:46 PM";
string customFormatForSecondDateTimeString = "M/dd/yyyy hh:mm:ss tt";
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");
Trying to parse s I get Exception
"String was not recognized as a valid DateTime"
string s = #"07/24/2014 14:46:47";
DateTime dt = DateTime.Parse(s);
System.Globalization.CultureInfo provider =
System.Globalization.CultureInfo.InvariantCulture;
string dateTimeString = #"07/24/2014 14:46:47";
string dateTimeFormat = #"MM/dd/yyyy HH:mm:ss";
var dateTime = DateTime.ParseExact(dateTimeString, dateTimeFormat, provider);
Useful links:
DateTime.ParseExact Method on MSDN
Standard Date and Time Format Strings on MSDN
The "/" custom format specifier on MSDN
Using the Escape Character on MSDN
DateTime myDate = DateTime.ParseExact("7/24/2014 14:46:47",
"MM/dd/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
As said by Sayse and Sriram Sakthivel you need to pass in the format the string will be in
DateTime.ParseExact(s, "MM-dd-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
I would like to have my end result in date format as per the specified format i.e YYMMDD how can i get this from a string given as below
string s="110326";
From string to date:
DateTime d = DateTime.ParseExact(s, "yyMMdd", CultureInfo.InvariantCulture);
Or the other way around:
string s = d.ToString("yyMMdd");
Also see this article: Custom Date and Time Format Strings
DateTime dateTime = DateTime.ParseExact(s, "yyMMdd", CultureInfo.InvariantCulture);
although id recommend
DateTime dateTime;
if (DateTime.TryParseExact(s, "yyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out dateTime))
{
// Process
}
else
{
// Handle Invalid Date
}
To convert DateTime to some format, you could do,
string str = DateTime.Now.ToString("yyMMdd");
To convert string Date in some format to DateTime object, you could do
DateTime dt = DateTime.ParseExact(str, "yyMMdd", null); //Let str="110719"
I have a date of the following format in a file (YYYY-MM-DD HH:MM:SS.millisecs):
1987-04-03 19:17:12.000
When I use DateTime to parse this string, it gets only the date part and does not get the time part. Can someone please tell me how to parse this into the DateTime object?
Use DateTime.ParseExact().
var format = "yyyy-MM-dd hh:mm:ss.fff"
var dt = DateTime.ParseExact(s, format);
See http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx. You should also add a format provider, just as CultureInfo.InvariantCulture.
var dt = DateTime.ParseExact(s, format, CultureInfo.InvariantCulture);
I did up a quick console app and it's showing both date and time:
string dateTimeString = "1987-04-03 19:17:12.000";
Console.WriteLine(DateTime.Parse(dateTimeString));
Console.ReadLine();
The resulting output is:
4/3/1987 7:17:12 PM
You might be using the resulting parse value incorrectly?
DateTime.Parse("1987-04-03 19:17:12.000") returns 4/3/1987 7:17:12 PM
The format string you are looking for is:
yyyy-MM-dd HH:mm:ss.fff
So
DateTime myDateTime = DateTime.ParseExact(sourceString, "yyyy-MM-dd HH:mm:ss.fff",
CultureInfo.InvarientCulture);
See MSDN for more information
How are you parsing it? How are you converting the DateTime to a string?
DateTime date = DateTime.Parse ("1987-04-03 19:17:12.000");
Console.WriteLine (date);
// yields: 4/3/1987 7:17:12 PM
Console.WriteLine (date.Date);
// yields: 4/3/1987
IFormatProvider culture = new CultureInfo("en-US", true);
DateTime.ParseExact(timeString, "dd/MM/yyyy HH:mm:ss.fff",culture);