I am parameterising my test cases by using data read from .csv files. One of the columns in the csv file has simple date values (as regular strings) in US format, eg mm/dd/yyyy. When the data is actually read and populated into a TestContext however, TestContext.DataRow["MyDateColumn"] actually returns a converted System.DateTime object, complete with a timestamp of 12:00:00 AM. I absolutely do not require or want this automatic conversion. How do I stop this from happening?
If the type of the MyDateColumn is set to datetime, then it would/should return a datetime object.
Try changing the type of MyDateColumn to be string and see if that does the trick.
UPDATE
Change dates in the CSV so that they are "mm/dd/yyyy" instead of mm/dd/yyyy.
DateTime.Parse(TestContext.DataRow["MyDateColumn"], CultureInfo.InvariantCulture).ToShortDateString()
I got solution for this :)
I just put ' before actual data. When I retrieve data, data comes as is. Before using it, I remove ' from data with substring method.
I remember using this technique on excel to make numbers appear as text. It worked for me.
I put double qoutes around the data. That gives back strings, without removing the quotes.
columnInt, columnString
1, "11.12.89"
2, "12.12.89"
Related
Context: I'm serializing some data that includes a date of which I need to display milliseconds. I'm using a XmlSerializer to write an XML file. I'm taking some data from a database using a DataTable.
What I'm actually doing: I cast the date in this way
CDate(dr("MyDate")).ToUniversalTime()
But once serialized this does not includes milliseconds.
While trying some workarounds I've found out that serialize in the same way
DateTime.Now.ToUniversalTime()
does includes milliseconds.
Actual question: what should I do to include milliseconds while serializing dr("MyDate")?
Please note that answers in VB.NET or C# are both well accepted.
Have you checked, what CDate contains? For exmaple Definition of CDate shows that CDate contains only seconds, not milliseconds. When the original time in the database is stored in text format (with milliseconds) you should use DateTime.TryParseExact(...) to get the complete time value. Otherwise, when the database itself uses another DateTime-class, you should use this type directly to convert to datetime.
I have column F that I want to format as money so add $ sign and commas. It's to late for me to attempt to format it in the Source so I was going to format it via C# after all data has been written to Excel. I know you can use this syntax to change a column to a date - what would be the appropriate syntax for money?
range.EntireColumn.NumberFormat = "MM/DD/YYYY";
Sorry for my stupidity on this one...it's actually quiet simple
range3.EntireColumn.NumberFormat = "$#,##0.00";
I have a string date like 'Wednesday, May 15, 2013' when I Parse it o lost the original format, is there a way to know what was the original format date or get it before the final Parse?
No, you'll lose the original format, if will now be a different object type with no care for its original format prior to parsing.
Your best option would be to store the pre-parse format prior to parsing as a different variable.
However if you simply wish to format the date in that original format, see Farhad's answer.
You can use this for getting date in a fromat you wish.
String.Format("{0:D}", DateTime.Now); // Tuseday, May 21, 2013
The DateTime struct does not have a format, it stores all of those values on various properties. When you want to display it you specify which format to use.
Using a format specifier when displaying it will likely do what you want. This mdsn article provides some basic information on format specifiers. The only way you can use the exact format you show there is if you know it ahead of time and have a format specifier for it. If you know you'll want to display strings in that format throughout the program it will be easy, if you get many different formats and want to decide how to display your dt at runtime it will be fairly complicated. I'm sure you could write some code to figure out what it is, but once the DateTime is created it will have no notion of what format the string used to create it was in.
I'm having an issue with date/time formats in ASP.NET/C#. I have my SQL Server database set up with a PostDate field set to a type of "datetime". But it's saving the date in a strange format. I added a new row through a form and I got this as the date/time string:
2012-09-28 14:56:48.910
When it gets parsed by JSON.NET it gets even stranger. I get:
2012-09-28T14:56:48.91
The date and time are obviously correct, but how do I set things so that I can parse the date into a human-friendly way? There isn't really any code to post because the date is being added when the row is inserted. I'd like to format this as "Sept. 28, 2012 2:56 pm". How do I do that? Do I need to format the string before or after it's parsed as JSON?
That's not a "strange" format at all. The second form is ISO-8601; the first is ISO-8601 without the T. Considering the strange formats you can get in JSON, it looks like you've been let off pretty lightly!
Serialization formats aren't meant to be user-friendly, particularly - they're meant to be machine-to-machine formats.
I would hope that JSON.NET would give you a DateTime after parsing; it should only be giving you the ISO-8601 format after you've converted back to JSON.
If you've got a DateTime that you want to format for user consumption, there are all kinds of options with standard and custom format strings. Don't forget that you should respect the culture of the user, as far as possible - so make sure you're taking appropriate steps to either set the thread's current culture to be the user's one, or that you're passing the culture explicitly to DateTime.ToString etc.
You can try it in C#:
.ToString("MMM d yyyy, h:mm tt")
I am receiving some data into a variable of type object. In certain cases this data are date values. For that data, I would like to convert this to a string and return it in the same format as it was passed. In some cases, the object could be a datetime, in others a date only or time only values.
As soon as I convert the object to a date or a string, it is obviously given a time of midnight which in my scenario may be a valid time (so I cannot test to see if the time is midnight in which case I could deduce that it would have been a date only date value, nor can I use regex on it as there will always be a time element).
Intellisense shows me it correctly, ie in the format I am wishing to return the value.
Is there an easy way to achieve this (hopefully without using reflection)
Many thx
Simon
Your question is a little unclear but I think you're looking for something like this:
DateTime result;
if (DateTime.TryParse(value, out result))
{
// use result here
}
In the above code value is a string that represents the data coming in. The code will only enter the if block if the string is a valid DateTime. At which point you can do the processing you need on it.
Im not sure i understand the question but i would recommend you to take a look at this conversion example on MSDN, and see the Documentation of the DateTime Structur it contains a lot of Conversion/Formatting Methods i hope it helps.
There are many way to do formatting on the datetime and one of the simple way is fetch the data from the required table in the desired format. Like here you need to display the date and if you your format is dd/MM/yyyy then try this
select Convert(varchar(10),StartDate,103) as StartDateformat from table where filtername=#filtername
use this link to find other format Cast and Convert
From local variable to DateTime Conversion
DateTime todateformat = Convert.ToDateTime(txttodate.Text.Trim());
From DateTime to local variable Conversion in specific format
string startdate = todateformat.ToString("dd/MM/yyyy hh:mm:ss");