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.
Related
I am using the C# Object Persistence model in my application, and am populating the table from an external source. I insert a row with the following date (in a String column): 2018-12-12T22:27:14.73Z. This is generated from a Timestamp using the following Go code:
&dynamodb.AttributeValue{S: aws.String(entity.TimeStamp.UTC().Format("2006-01-02T15:04:05.999Z"))}
However, the DynamoDB Object Persistence model chokes when trying to convert it to a System.DateTime, with the following error: System.InvalidOperationException: Unable to convert [2018-12-12T22:27:14.73Z] of type Amazon.DynamoDBv2.DocumentModel.Primitive to System.DateTime
If I let my service write a System.DateTime (using a POCO that contains a DateTime property), it looks something like this: 2018-12-19T07:45:36.431Z. What am I missing that prevents AWS from properly deserializing my dates? It looks like I'm writing them in the same format that Amazon is writing them?
Ok, I found the answer. DynamoDB expects the column to always have 3 digits of precision in the fractional part of the timestamp. When I formatted the time from Golang, I used "2006-01-02T15:04:05.999Z" as my format string, but this causes time.Format to truncate the trailing zeros (when they exist). Changing my time format string to always print the full precision, I was able to fix my issues.
That said, the documentation could be a little more clear about this precision requirement. Or the exception could be a little more explicit. Heres hoping this question/answer makes that exception searchable on Google!
I am serializing a DateTime object using the DataContractSerializer class. I get the following output.
2013-05-21T10:50:23.5602265-04:00
What is the final component?
YYYY-MM-DDTHH:MM:SS.zzzzzzz-??:??
Thanks
That is your time zone offset. Greenwich Mean Time - 4 hours.
If I write the C# code below, I get the current time in the same format as is described:
DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffffffK")
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 trying to simply change the date format from the datatable to universal time format but it formats it wrongly as if I have date for August 7 it changed it to August 8 after formatting it to universal date time. My code for formatting date is,
DateVar[runs] = DateTime.Parse(Convert.ToString(output.Tables[0].Rows[runs][0])).ToUniversalTime().ToString();
Don't get in to code its correct and its a part of loop so "run" is loop and output is data set having one table I have first data in table is "Sunday, August 07, 2011 10:52 PM" and it was converted to "8/8/2011 5:52:00 AM" after implementing universal time format.
Hopes for your suggestions
Universal time isn't a format - it's a time zone, effectively. It's not clear what you're trying to do, but converting a "local" DateTime to "universal" DateTime will usually change the time. If you don't want that to happen, don't call ToUniversalTime.
It's a pity that the .NET date/time API isn't as clear as it could be - the DateTime type itself has some horrible ambiguities about it. I'm trying to improve the situation with my Noda Time project, but you will need to understand what time zones are about etc.
Personally I would suggest not using simply DateTime.Parse or just calling ToString unless you're absolutely sure that the default format is what you want. I usually call DateTime.ParseExact and specify the expected format (and usually CultureInfo.InvariantCulture unless it's a user-entered string) - and likewise I provide a format string to the ToString call.
In your code you're simply converting a string to a string - what are you attempting to accomplish? If you're just trying to change the format (e.g. to dd/MM/yyyyTHH:mm:ss) then you don't need to call ToUniversalTime but you do need to provide the format string.
I suggest you split your code out into several statements to help you debug this (and for general code clarity):
Fetch the string from the DataTable, if you really need to (if it's already a DateTime, there's no point in converting it to a string and then back again)
Parse the string (again, assuming you need to)
Perform any conversions you need to
Format the DateTime with an explicit format string
Now if any single operation is causing a problem, you can isolate it more easily.
If I run ToUniversalTime() from Greenwich it will give same time but if i do it while I live some where else it will get an offset date time object of + or - hours depending on position.
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"