I need to convert a string format dd/mm/yy to a datetime.
I tried with:
DateTime d;
if (DateTime.TryParseExact(dateStr.Trim(), "dd/MM/yyyy", new CultureInfo("es-es"), DateTimeStyles.AssumeLocal, out d))
return d;
Your code seems ok, but also you can convert a string into datetime like below
DateTime d = DateTime.ParseExact("11/02/2016", "dd/MM/yyyy", CultureInfo.InvariantCulture);
return d;
if you have string as dd/MM/yy format and need to convert it as dd/MM/yyyy fomat datetime then you can do like this
DateTime temp = DateTime.ParseExact("11/02/16", "dd/MM/yy", CultureInfo.InvariantCulture);
DateTime d = DateTime.ParseExact(temp.ToString("dd/MM/yyyy"), "dd/MM/yyyy", CultureInfo.InvariantCulture);
return d
string dateBirth = ddlDay.SelectedItem.ToString() + "/" + ddlMonth.SelectedItem.ToString() + "/" + ddlYear.SelectedItem.ToString();
DateTime dt = DateTime.ParseExact(dateBirth, "dd/MM/yyyy", null);
Related
string s = "20100426T000000Z";
DateTime date = DateTime.ParseExact(s, "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture);
Tried this getting invalid string format issue. Can somebody help me in this.
This should do it :
string s = "20100426T000000Z";
DateTime theDateTime = DateTime.ParseExact(s, "yyyyMMddTHHmmssZ", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
I have a date time format of yyyyMMdd:HHmmss. I receive the datetime from a device and I can confirm that the format is yyyyMMdd:HHmmss.
For example I get datetime as 20180331:162308. I tried to convert to 03/31/2018 04:23 PM format by using below code:
string localDateTime =
Convert.ToString(protocol.GetParameter(Parameter.localmdmdatetimestr_3043));
string formatString = "yyyyMMdd:HHmmss";
protocol.Log(
"QA" + protocol.QActionID + "|DateTime|localDateTime" + localDateTime,
LogType.Error,
LogLevel.NoLogging);
DateTime LocalDt = DateTime.ParseExact(
localDateTime,
formatString,
CultureInfo.InvariantCulture);
But instead get
System.FormatException: String was not recognized as a valid DateTime
error.
As per your requirement
string localDateTime = Convert.ToString("20180331:162308");
string formatString = "yyyyMMdd:HHmmss";
DateTime LocalDt = DateTime.ParseExact(
localDateTime,
formatString, CultureInfo.InvariantCulture);
Console.WriteLine(LocalDt);
var test = LocalDt.ToString("yyyy/MM/dd HH:mm tt", CultureInfo.InvariantCulture);
Console.WriteLine(test);
Console.ReadLine();
I have a string which is not in a datetime format eg:20160503. How can i change it to Datetime. I tried using Substring. It is working.Is there any more efficient way? Below is what I have right now.
string a = "20160503";
int b = Convert.ToInt32(a.Substring(0, 4));
int c = Convert.ToInt32(a.Substring(4, 2));
int d = Convert.ToInt32(a.Substring(6, 2));
string date = b + "/" + c + "/" + d;
DateTime result = new DateTime();
DateTime.TryParse(date, out result);
Since you know the exact format of your datetime, you could try to use the ParseExact DateTime's method.
var dt = DateTime.ParseExact(a,"yyyyMMdd",CultureInfo.InvariantCulture);
For further info, please have a look here.
Try somthing like this:
Define your own parse format string to use.
string formatString = "yyyyMMdd";
string sample = "20160503";
DateTime dt = DateTime.ParseExact(sample,formatString,null);
Thanks for your replies. Finaly I ended up using DateTime.TryParseExact
string dateString = "20150503";
DateTime dateValue = new DateTime();
DateTime.TryParseExact(dateString, "yyyyMMdd", new CultureInfo("en-US"), DateTimeStyles.None, out dateValue);
Well I've tried following:
IFormatProvider culture = new CultureInfo("en-US", true);
DateTime DateTo;
string DateToStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
DateTo = DateTime.ParseExact(DateToStr, "yyyy-MM-dd HH:mm:ss", culture);
Also tried following:
DateTime DateTo;
string DateToStr = DateTime.Now.ToString("yyyy-MM-dd");
DateTo = DateTime.ParseExact(DateToStr, "yyyy-MM-dd", culture);
And following also:
DateTime DateTo;
string DateToStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
DateTo = DateTime.ParseExact(DateToStr, "yyyy-M-dd HH:mm:ss", culture);
Each time it is giving me the same error:
String was not recognized as a valid DateTime
Whats going wrong here?
Thanks in advance.
Try calling it with additional parameter
DateTo = DateTime.ParseExact(DateToStr, "yyyy-MM-dd HH:mm:ss", culture, DateTimeStyles.AllowWhiteSpaces);
Say I have the following:
string fromTime = "8:00am";
string someDate = "06/01/2012";
I want to end up doing this:
DateTime dt = Convert.ToDateTime(someDate + someTime);
Basically I want to take a date and add the time to it with the result for the above to be
06/01/2012 8:00am
But have it stored in a datetime variable. Is this possible?
You could use the DateTime.TryParseExact method which allows you to specify a format when parsing:
class Program
{
static void Main()
{
string s = "06/01/2012 8:00am";
string format = "dd/MM/yyyy h:mmtt";
DateTime date;
if (DateTime.TryParseExact(s, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
Console.WriteLine(date);
}
}
}
Try this:
DateTime dt = DateTime.ParseExact(someDate + " " + someTime, "MM/dd/yyyy h:mmtt", CultureInfo.InvariantCulture);