I would like to convert the String "2018-05-10 10-AM" to a DateTime-Object. I tried everything, including this (is throwing an Exception):
var date = DateTime.ParseExact("values[0]", "yyyy-MM-dd HH-tt",
System.Globalization.CultureInfo.InvariantCulture);
Anybody knows what I am doing wrong?
I think you should hh specifier instead of HH specifier since your hour part is 12-hour clock format, not 24-hour clock.
var date = DateTime.ParseExact("2018-05-10 10-AM", "yyyy-MM-dd hh-tt",
CultureInfo.InvariantCulture);
And also as David commented, you have a typo in your code. At least it should be like;
var date = DateTime.ParseExact(values[0], "yyyy-MM-dd hh-tt",
CultureInfo.InvariantCulture);
You can also use something like this:
var date = DateTime.ParseExact("2018-05-10 10-AM", #"yyyy-MM-dd hh-tt", new CultureInfo("en-US"));
and in your own example:
var date = DateTime.ParseExact(values[0], #"yyyy-MM-dd hh-tt", new CultureInfo("en-US"));
note that don't put variables in quotations.
Related
Parse Time not working as I want to convert "13-06-2019 00:00:00"(dd-MM-yyyy HH:mm:ss) to "06-13-2019 00:00:00"(MM-dd-yyyy HH:mm:ss)
tried with Convert.toDateTime() and DateTime.ParseExact()
IFormatProvider culture = new CultureInfo("en-US");
var a = DateTime.ParseExact(a, "MM-dd-yyyy hh:mm:ss", CultureInfo.InvariantCulture);
var b = DateTime.ParseExact(a, "yyyy-MM-dd HH:mm:ss", null);
var c = DateTime.ParseExact(a, "yyyy-MM-dd HH:mm:ss", culture);
Nothing working in it
DateTime structure uses Gregorian calendar under the hood and there is no 13th month in that calendar.
So, parsing 13 with MM specifier is wrong. I strongly suspect that you try to use dd-MM-yyyy HH:mm:ss format instead.
string a = "13-06-2019 00:00:00";
DateTime b = DateTime.ParseExact(a, "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
Here a demonstration
Your second and third examples also don't work since their formats are completely different than your string. When you parse your string with ParseExact method, your strings and your format should match exactly.
Also I want to mention that, both hh and HH specifiers would work in my code example. But as a general format consideration, using dd-MM-yyyy HH:mm:ss format is much more common and reliable than the other option.
The format parameter of DateTime.ParseExact(date,format,culture) is the source format of the date string to be converted and the return value is type date which you can convert back to string as per the desired format.
var a = "13-06-2019 00:00:00";
IFormatProvider culture = new CultureInfo("en-US");
DateTime b = DateTime.ParseExact(a, "dd-MM-yyyy hh:mm:ss", CultureInfo.InvariantCulture);
Console.WriteLine($"{b:MM-dd-yyyy HH:mm:ss}");
In my application , i have a control which is displaying current time in HH:mm:ss format. My question is if format is HH mm ss then, how this format is converted back to HH:mm:ss format
ex
03-08-2016 10:30:45 ==> my required format
03-08-2016 10 30 45 ==> current format. or it may be any other format
How can i get required format?...
DateTime class has a .ToString() method which accepts format string as it's argument. Please have a look at this
You can get your required format as follows:
var now = DateTime.Now;
var formattedDate = now.ToString("dd-MM-yyyy HH mm ss");
Let the dateInput be the date time object, and the dateFormatString be the required format. then you can use the .ToString() method to get the formatted date string; This can be achieved by using the following code:
string dateFormatString="dd-MM-yyyy HH:mm:ss";
string formattedDateString = dateInput.ToString(dateFormatString);
Suppose your current date time format dd-MM-yyyy HH mm ss like 03-08-2016 10 30 45 then you can convert your current format to dd-MM-yyyy HH:mm:ss as follow
var dt = DateTime.ParseExact("03-08-2016 10 30 45", "dd-MM-yyyy HH mm ss", CultureInfo.InvariantCulture);
string newdate = dt.ToString("dd-MM-yyyy HH:mm:ss");
using same mechanism you can convert any format to another
Let's say you can get the string value from your control that displays in MM-dd-yyyy HH mm ss format, you can use DateTime.ParseExact Method to convert it back to a DateTime variable.
var datetime = DateTime.ParseExact(controlDT.Text, "MM-dd-yyyy HH mm ss", CultureInfo.InvariantCulture);
var newDateTimeString = datetime.ToString("MM-dd-yyyy HH:mm:ss");
Easy way around. Try this:
DateTime date = DateTime.Now;
string dt = date.ToString("dd-MM-yyyy HH:mm:ss");
The output produced will be (as per your required format):
03-08-2016 05:25:47
Convert like this:
var date= DateTime.Now;
var Yourrequeired= date.ToString("dd-MM-yyyy HH: mm: ss");
I try to parse a date providing a specified format with the following code :
var date = "30/06/2014";
var ret2 = DateTime.ParseExact(date, "dd/mm/yyyy", null);
var ret3 = DateTime.ParseExact(date, "dd/mm/yyyy", CultureInfo.CurrentCulture);
var ret4 = DateTime.ParseExact(date, "dd/mm/yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(ret2);
Console.WriteLine(ret3);
Console.WriteLine(ret4);
-----OUTPUT-----
30/01/2014 00:06:00
30/01/2014 00:06:00
30/01/2014 00:06:00
Could someone explain me why this code doesn't return the 30/06/2014 00:00:00 value I am expecting ?
mm is the placeholder for minutes. You should use MM for months
mm means minutes, not months. Try using MM instead.
As mentioned, mm represents Minutes - note that the minutes of each DateTime is 06:00 instead of the default 00:00, and 1 is the default for a month(since there is no 0 month).
See MSDN on Standard and Custom DateTime format strings for more info, as well as more helpful placeholders.
the code used the wrong format string
var ret2 = DateTime.ParseExact(date, "dd/mm/yyyy", null);
var ret3 = DateTime.ParseExact(date, "dd/mm/yyyy", CultureInfo.CurrentCulture);
var ret4 = DateTime.ParseExact(date, "dd/mm/yyyy", CultureInfo.InvariantCulture);
mm is placeholder for minutes.
to get month, use MM in the format string
var ret2 = DateTime.ParseExact(date, "dd/MM/yyyy", null);
var ret3 = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.CurrentCulture);
var ret4 = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);
check out the example on MSDN https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx
I have DateTime variable in this format "dd/mm/yyyy hh:mm:ss".
I would like to convert this to "mm/dd/yyyy hh:mm:ss". (Month before day)
I tried to use DateTime.parse like this:
DateTime dt = new DateTime();
dt = dateTimePicker1.Value;
dt = DateTime.Parse(dt.ToString(), "mm/dd/yyyy", null);
but it wont work.
I also try it with DateTime.ParseExact() but still nothing happened.
Any suggestion?
A DateTime doesn't have any implicit format. It just has date and time values.
Formatting subject only applies when you try to get it's textual representation. If you wanna get string represetation of it, you can use .ToString() method like;
string s = dateTimePicker1.Value.ToString("MM/dd/yyyy hh:mm:ss",
CultureInfo.InvariantCulture);
And remember, mm specifier is for minutes, MM specifier is for months. Also hh specifier is for 12-hour clock and HH specifier is for 24-hour clock representations. You might wanna use HH instead.
I used InvariantCulture as a second parameter because / and : have special meaning as replace me current culture or supplied culture date or time separator.
string date = dateTimePicker1.Value.ToString("MM/dd/yyyy");
You can also use this below code for advance:
GregorianCalendar cal = new GregorianCalendar(); // or any type of calendar you want ...
string formatted = string.Format("{1:00}/{2:00}/{0} {3:00}:{4:00}:{5:00} ",
cal.GetYear(dt), cal.GetMonth(dt), cal.GetDayOfMonth(dt) , cal.GetHour(dt), cal.GetMinute(dt), cal.GetSecond(dt));
DateTime time = DateTime.Now;
string format = "MMM ddd d HH:mm yyyy";
Console.WriteLine(time.ToString(format));
I am trying to convert a string value into DateTime. It gives me an error as, specified string is not in correct format.
Here is the code,
DateTime myDate = DateTime.ParseExact("07-09-2013 01:14:14:1414", "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
string strDate = "07/09/2013 01:04:02:4";
Convert.ToDateTime(strDate);
DateTime dt = DateTime.Parse(strDate);
Please help in converting the same.
Thanks
Your format seems to be incorrect. Should be:
"dd-MM-yyyy HH:mm:ss:ffff"
Update. If number of digits representing fractions of a second varies, than the best bet is
"dd-MM-yyyy HH:mm:ss:FFFFFFF"
Refer to MSDN for other options for custom time and date formats.
Try in your page_load event:
Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo("tr-TR")
Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("tr-TR")
First, you mixed you years place
07-09-2013 is dd-MM-yyyy format
second, you need a :ffff after seconds
So the final line should be
DateTime myDate = DateTime.ParseExact("2013-07-09 01:14:14:1414", "yyyy-MM-dd HH:mm:ss:ffff", System.Globalization.CultureInfo.InvariantCulture);
Your format isn't correct:
"07-09-2013 01:14:14:1414"
"yyyy-MM-dd HH:mm:ss"
Atleast your date is the other way around, and the milliseconds is not specified.
Correct you format according to this: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
For the downvoter:
The correct format is specified by Andrei: "dd-MM-yyyy HH:mm:ss:ffff"
First of all, you should change your date format to dd-MM-yyyy because it fits with your date format.
Second of all, for miliseconds part, you can use . instead of :
DateTime myDate = DateTime.ParseExact("07-09-2013 01:14:14.1414", "dd-MM-yyyy HH:mm:ss.ffff", System.Globalization.CultureInfo.InvariantCulture);
string strDate = "07/09/2013 01:04:02.4";
Convert.ToDateTime(strDate);
DateTime dt = DateTime.Parse(strDate);
Here a DEMO.
For more information, check Custom Date and Time Format Strings