I am reading from excel file and save the content into database
one of the column contains length of video in this format
HH:mm:ss
I write this code so far
string time = oledbReader[6].ToString();
DateTime streamingTime = DateTime.ParseExact(time,
"HH:mm:ss",
System.Globalization.CultureInfo.CurrentCulture);
I am getting error:
String was not recognized as a valid DateTime.
I tried debug mode and I see the value :"30/12/1899 00:09:21" in the Variable time
when the value in the current execl column is:"00:09:21"
Where does the "30/12/1899" came from? Why is the string was not recognized as a valid DateTime?
Can I save only the format HH:mm:ss into sql server?
Try this, easy hack as my comment above.
string time = oledbReader[6].ToString().Split(" ".ToCharArray())[1];
DateTime streamingTime = DateTime.ParseExact(time, "HH:mm:ss",System.Globalization.CultureInfo.CurrentCulture);
or you could parse it as it is...
DateTime streamingTime = DateTime.ParseExact(time, "dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
Since you didn't gave us any information about your CultureInfo, here with InvariantCulture;
string time = "30/12/1899 00:09:21";
DateTime streamingTime = DateTime.ParseExact(time, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
Console.WriteLine(streamingTime);
Output will be;
12/30/1899 12:09:21 AM
Here a DEMO.
For more informations, check out Custom Date and Time Format Strings
Use a TimeSpan structure to hold a time value. A DateTime includes both a date and a time.
The problem is that excel not have time field and automatically convert time to date time field.
If course date is "empty" which means that it its a minimal date for excel - 30/12/1899
Moreover you don't have to invoke ToString methods because object is already a DateTime
Related
my code below throw an exception as invalid date time. the error occur after i publish it to my server. working find at my developing PC
string str = "27-07-2015 6:15 pm";
DateTime dt = Convert.ToDateTime(DateTime.ParseExact(str, "dd-MM-yyyy h:mm tt", null).ToString("dd-MM-yyyy hh:mm tt"));
it takes '27' as month and '7' as day.
what i did to solve the problem:
i already update the datetime format on that server to dd-MM-yyyy
i double checked the capital and small letter of the date time format.
change the 'null' to 'CultureInfo.InvariantCulture'
change 'pm' to 'PM', 'tt' to 'TT'
read through all the resources i could find on google and stackoverflow, nothing's help.
am i missing something here? i know i did... :(
As #Rawling correctly noted, you're parsing the datetime twice: first, using your custom formatting, and second, using the system's default formatting.
This is silly - you already have the DateTime from the ParseExact method:
string str = "27-07-2015 6:15 pm";
var dt = DateTime.ParseExact(str, "dd-MM-yyyy h:mm tt", null);
That's it, you're done. No need to convert to string again, and parse that once more (and even worse, using the same custom formatting to do the ToString, so the subsequent Convert.ToDateTime is bound to fail anywhere that's not the default datetime formatting).
There are a few possibilities;
Let's analyze your DateTime.ParseExact(str, "dd-MM-yyyy h:mm tt", null) part first. This will be parsed your 27-07-2015 6:15 pm string successfully if;
Your CurrentCulture's TimeSeparator is : and
Your CurrentCulture's PMDesignator is PM (not empty string)
If both are okey, you have successfully parsed a DateTime.
After that, you generate it's textual representation with dd-MM-yyyy hh:mm tt format. And that's still depends on your CurrentCulture, your result might have PM or not. For both case, there is no guaranteed to parse your string with Convert.ToDateTime method because it will be parsed your string only if it is a standard date and time format of your CurrentCulture.
On the other side, what you do doesn't make sense to me. You parse your string first, then you generate string representation of it, then you try to parse this string again. Doesn't make sense, right?
I strongly suspect you just need;
string str = "27-07-2015 6:15 pm";
DateTime dt = DateTime.ParseExact(str, "dd-MM-yyyy h:mm tt", CultureInfo.InvariantCulture);
I am trying to convert the string to DateTime. But I can not convert.
DateTime dt = DateTime.Parse("16/11/2014", CultureInfo.InvariantCulture);
Console.WriteLine("Date==> " + dt);
The error is FormatException.
My input time format is "dd/MM/yyyy".
Please let me any idea to resolve my problem.
Given that you know your input format, you should specify it with `ParseExact:
DateTime dt = DateTime.ParseExact(text, "dd/MM/yyyy",
CultureInfo.InvariantCulture);
I would always recommend being as explicit as you can be about date/time formats. It makes your intention very clear, and avoids the possibility of getting months and days the wrong way round.
As Soner has stated, CultureInfo.InvariantCulture uses MM/dd/yyyy as its short date pattern, as you can validate with:
Console.WriteLine(CultureInfo.InvariantCulture.DateTimeFormat.ShortDatePattern)
As a mild plug, you might want to consider using my Noda Time project for your date/time handling - aside from anything else, that allows you to treat a date as a date, rather than as a date and time...
Because InvariantCulture doesn't have dd/MM/yyyy as a standard date and time format, but it has MM/dd/yyyy as a standard date and time format.
That's why it thinks your string is MM/dd/yyyy format, but since there is no 16 as a month in Gregorian calender, you get FormatException.
Instead of that, you can use DateTime.TryParseExact method to specify exact format like;
string s = "16/11/2014";
DateTime dt;
if(DateTime.TryParseExact(s, "dd/MM/yyyy", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
}
So last week, the way our application worked was that the back-end team was storing dates as a string in YYYY-MM-DD formats. This week, they changed it to be a DateTime object instead of a string. So now I'm just creating a DateTime object from the string value on this particular DateTime control we use.
So basically with our custom control , it was like this:
mySearchModelObject.fromDate = myDateRangeControl.Values[0]; //string
mySearchModelObject.toDate = myDateRangeControl.Values[1]; //string
Now it's more like this:
DateTime fromDate, toDate;
DateTime.Tryparse(myDateRangeControl.Values[0], out fromDate);
DateTime.Tryparse(myDateRangeControl.Values[1], out toDate);
mySearchModelObject.fromDate = fromDate;
mySearchModelObject.toDate = toDate;
But searching with the same date range as last week yields different results from the DB.
I'm wondering if it's because our dates were "YYYY-MM-DD" as strings, but now it's getting a date time object in whatever the system's format is + the time itself.
So is there a way to format my DateTime object to still have it in the same YYYY-MM-DD format?
Use DateTime.TryParseExact and provide "yyyy-MM-dd" as format string.
See http://msdn.microsoft.com/en-us/library/h9b85w22
you can do fromDate.ToString("yyyy-MM-dd") to have a date formatted as YYYY-MM-DD
see msdn on standard and custom datetime format
see demo https://dotnetfiddle.net/NZz0HG
also if mySearchModelObject.fromDate is of type object, you can assign a DateTime or a string, no compiler warnings/error.
But when is used, maybe with mySearchModelObject.fromDate.ToString() you get a different result, before was '2014-12-31' and now 12/31/2014 12:00:00 AM
I have a string Date which is coming from Database I need to parse or convert that String Date in DateTime form. Sharing the data and code as of now sharing the date which is coming from DB
String Date="7/19/2010 7:34:43 AM";
// I am parsing in DateTime form by below code
Date= DateTime.Parse(Date).ToString("yyyy-MM-dd HH:mm:ss.fff");
But I am getting the error while parsing with existing code as String was not recognized as a valid DateTime.
Can anyone please share some info how can I resolve this error so that I wont receive any exception
Note
Issue with my code is the date which is coming from Database is not a valid string type that's why I am getting the error string is not recognized as valid datetime
You should do, using DateTime.ParseExact
DateTime dt = DateTime.ParseExact("7/19/2010 7:34:43 AM",
"M/d/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture);
DateTime.ParseExact(yourstring, "yyyyMMdd_HH:mm:ss.fff", new CultureInfo("en-US"));
Will solve your issue.
For reference follow :
Date Time Parse
Date Time Parse Exact
Either you can use
DateTime.ParseExact or DateTime.TryParseExact
This will allow you to specify specific formats.
I prefer the TryParseExact as they provide good coding style for the casing error.
This is giving an exception:
String was not recognized as a valid DateTime.
string format = "MM/dd/yyyy hh:mm:ss.fff";
string dt_db1 = DateTime.ParseExact(txtTenureFrom.Text, "dd/MM/yyyy",
CultureInfo.InvariantCulture)
.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
DateTime d1 = DateTime.ParseExact(dt_db1, format, CultureInfo.InvariantCulture);
You didn't specify the input data, but the first part looks inconsistent with the second.
You start with a date value you expect to be in dd/MM/yyyy format, without a time component.
You convert it to a date value in MM/dd/yyyy format, still without out a time component.
You then try to parse it again in MM/dd/yyyy hh:mm:ss.fff format, expecting a time component to somehow be introduced in the string???
Where do you expect the time to magically come from?