I have a string "03/13/13" and when i convert this to DateTime it is throwing error as invalid string format.
How to convert string "03/13/13" to DateTime "03/13/13" (Same Format)
Convert.ToDateTime("03/13/13", new CultureInfo("en-GB"))
Use DateTime.ParseExact with format "M/d/yy"
DateTime dt = DateTime.ParseExact("03/13/13", "M/d/yy", CultureInfo.InvariantCulture);
Where in format:
M - For single digit or double digit month
d - For single digit or double digit day
yy- for two digits year.
You may see: Custom Date and Time Format Strings
Later if you want the string representation in the same format you can do:
string str = dt.ToString("MM/dd/yy")
You can use Convert.ToDateTime or DateTime.Parse ..
DateTime date = Convert.ToDateTime("5/17/2012");
or
DateTime date1 = DateTime.Parse("5/17/2012");
Example:
string date = "5/17/2012";
DateTime dates = Convert.ToDateTime(date);
Related
I'm trying to Split this string: 2015-08-14 20:30:00
but the compiler shows this message:
Can't convert from String to Char
This is my code:
string date = reader["date"].ToString().Split("-").ToString();
The variable reader["date"] is an object, so I must convert it into a String. I want to Split the content into three other variable like this:
year: 2015
month: 08
day: 14
What am I doing wrong?
There is no String.Split overload that takes string as a parameter. That's why it looks closest overload which is char[] but there is no implicit conversation between them.
var array = "2015-08-14 20:30:00".Split(new char[]{'-', ' '});
will return
and you can get them with array[0], array[1] and array[2].
Also you can use to parse your string to DateTime instead (which your string is valid one) of splitting it like;
string s = "2015-08-14 20:30:00";
DateTime dt;
if(DateTime.TryParseExact(s, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
// dt.Year;
// dt.Month;
// dy.Day;
}
But since these properties are int, you will not get leading zeros for your single digit month and days.
In such a case, you can choose to use dd and MM custom date and time format specifiers.
Sometime a different approach should be considered. If your reader field datatype is date or datetime then using the correct datatype is the correct way to handle this info. A DateTime has already all you need.
DateTime dt = Convert.ToDateTime(reader["date"]);
int year = dt.Year;
int month = dt.Month;
int day = dt.Day;
As String.Split reaturns an array of strings you need date to be the same. So simply write
string[] date = Convert.ToString(reader["date"]).Split("-");
I have a date time string "12-24-2013 15:19:29" which is in "MM-dd-yyyy HH:mm:ss". I want to convert this string to datetime. But the format should not change.ie, it should be the same "MM-dd-yyyy HH:mm:ss" format.
When I used following method,
DateTime dt2 = DateTime.ParseExact(date31strin, "MM-dd-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
the format is changed to "dd-MM-yyyy HH:mm:ss". I have tried some other method also, but still getting this same format.
First, you should be parsing the string to a DateTime and then format it to the second format using ToString() method.
//Convert your string to a DateTime
DateTime dt2 = DateTime.ParseExact(date31strin,
"MM-dd-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
//If you want to change the format
string secondFormat = dt2.ToString("Second format string");
Note: Date is a Date and it does not have a format. If you need to convert the string to a DateTime, first line of code is enough
You are not changing the format.
You are parsing a string into a DateTime object which does not have a format.
When you decide to present the DateTime, you can format it any way you wish.
to parse String to DateTime use method DateTime.TryParse (http://msdn.microsoft.com/cs-cz/library/ch92fbc1%28v=vs.110%29.aspx) and then use DateTime formating (http://msdn.microsoft.com/en-us/library/az4se3k1%28v=vs.110%29.aspx) to output to aspx page
//frndzz if you are using j query and you don't convert date in(MM/dd/yyyy) to //(dd/MM/yyyy) then this code will help you
// i try it and i get the answer
string sd=txtmydate.Text //sd=04/27/2015 (MM-dd-yyyy) format
string [] sdate = sd.Split('-');
string fd = sdate[1];
fd=string.Concat(sdate[1],"-",sdate[0],"-",sdate[2]);
DateTime dt = Convert.ToDateTime(fd);
txtshow.Text = dt.ToString("dd/MM/yyyy");
//txtshow will show you date as 27-04-2015 (dd/MM/yyyy) format
thanks
How to convert string to DateTime:
string iDate = "Absent";
aEmployeeAttendence.Intime = DateTime.Parse(iDate);
//aEmployeeAttendence (object)
//Intime (field)
string strHijdt ="29-02-1435";
DateTime hdt = DateTime.ParseExact(strHijdt, "dd/MMM/yyyy HH:MI24",
CultureInfo.InvariantCulture);
Getting error while convert to string("29-02-1435") to datetime
2/1435 has 28 days only
so, below will work
string aa="28-02-1435";
DateTime hdt = DateTime.ParseExact(aa, "dd-MM-yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(hdt.ToLongDateString());
DEMO
since you have given input as 29-02-1435 even you provide correct date time format (dd-MM-yyyy) you will get error for the invalid date
Two problems here:
1. As mentioned above, expected format for does not match string (there is no time, different separator)
2. If your date string is in Hijri calendar, you should either provide correct culture explicitly or use system culture (pass null for IFormatProvider):
string strHijdt = "29-02-1435";
var culture = CultureInfo.GetCultureInfo("ar-SA");
DateTime hdt = DateTime.ParseExact(strHijdt, "dd-MM-yyyy", culture);
string formatString = "MMddyyyyHHmmss";
string sample = "20100611221912";
DateTime dt = DateTime.ParseExact(sample, formatString, System.Globalization.CultureInfo.InvariantCulture);
the specific exception thrown is:
System.FormatException: The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.
Your format should be:
string formatString = "yyyyMMddHHmmsss";
(It can also be "yyyyddMMHHmmsss", if it is 06-Noveber-2010)
Considering your Date is dt = {11/06/2010 10:19:12 PM} (11-June-2010)
For your current format:
MMddyyyyHHmmss
20100611221912
MM can't be 20, since MM stands for Month. So your code should be:
string formatString = "yyyyMMddHHmmsss";
string sample = "20100611221912";
DateTime dt = DateTime.ParseExact(sample, formatString, System.Globalization.CultureInfo.InvariantCulture);
If you didn't mean to import the 10th day of the 20th month of the year 611, either your format string or your data is wrong. Did you mean to import with "yyyymmddHHmmss" ?
I am developing windows application.
In that i have date in the string format as>> fileDate="15/03/2013"
I want it to be get converted into date format as my database field is datetime.
I used following things for it>>
DateTime dt = DateTime.ParseExact(fileDate, "yyyyy-DD-MM", CultureInfo.InvariantCulture);
DateTime dt = DateTime.Parse(fileDate);
Both of these methods proved failure giving me error>>
String was not recognized as a valid DateTime.
What can be mistake?
Is there another technique to do that?
string fileDate = "15/03/2013";
DateTime dt = DateTime.ParseExact(fileDate, "dd/mm/yyyy", CultureInfo.InvariantCulture);
You have to give the date format according to the date string you have to ParseExact. You can see more on Custom DateTime format - MSDN
Change
"yyyy-MM-dd HH:ss"
To
"dd/MM/yyyy"
Your code would be
DateTime dt = DateTime.ParseExact(fileDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
You should do this:
DateTime dt = DateTime.ParseExact(fileDate, "dd/MM/yyyy",CultureInfo.InvariantCulture);
You must pass in the string for the format ("dd/MM/yyyy") in the same style that you pass in the string fileDate.
u may try with this
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date convertedDate = dateFormat.parse("ur_dateString")
In your current code you are using format "yyyyy-DD-MM" which is wrong since date part require lower case d not upper case D. , Also for year part you are specifying 5 ys, it should be 4, like yyyy, the order according to your date string should be: "dd/MM/yyyy". To be on the safe side you can even use "d/M/yyyy", which would work for single digit or double digit day/month.
So your code should be:
string fileDate="15/03/2013";
DateTime dt = DateTime.ParseExact(fileDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
You can see more on Custom DateTime format - MSDN
It's because string "15/03/2013" cannot really be parsed as DateTime with format string "yyyy-MM-dd HH:ss".