Two-Way Date-String Conversion (in .Net)
If you need to store and retrieve dates as string (in a CSV file, for example), here's one way to handle it (in VB.net). (And if you know a better way -- please show me)
Const FORMAT As String = _
"yyyyMMddHHmmss" 'Example format
Dim s1 As String = "20040610140023"
Dim s2 As String
Dim d As Date
'Turn s1 into a date
d = Date.ParseExact _
(s1, FORMAT, _
System.Globalization.DateTimeFormatInfo.InvariantInfo)
'Turn the date back into a string
s2 = String.Format("{0:" & FORMAT & "}", d)
'Compare the starting and final strings
Debug.Assert(s1.Equals(s2), _
"The strings s1 and s2 should be equal")
'Davros' on Mon, 07 Jun 2004 21:38:09 GMT, sez: Can you give an example of this in C#?
'secretGeek' on Mon, 07 Jun 2004 22:56:57 GMT, sez: Hiya davros
here's an auto-converted version, thanks to the Babbelfisken tool:
const string FORMAT = "yyyyMMddHHmmss"
//Example format;
string s1 = "20040610140023";
string s2;
Date d;
//Turn s1 into a date
d = Date.ParseExact(s1, FORMAT, System.Globalization.DateTimeFormatInfo.InvariantInfo);
//Turn the date back into a string
s2 = string.Format("{0:" + FORMAT + "}", d);
//Compare the starting and final strings
Debug.Assert(s1.Equals(s2), "The strings s1 and s2 should be equal");
The babbelFisken tool is available here:
http://w1.311.telia.comjustinking73.webhost4life.com/u31115556/zip/MyUtils/BabbelFisken/Zexedir.zip
(link courtesy of http://blogs.wwwcoder.com/pradeepd/archive/2004/01/28/235.aspx)
there's also numerous free sites for providing such conversion -- but best of all is to learn to do it yourself!
cheers
lb
'Mike Entin' on Mon, 07 Jun 2004 23:15:14 GMT, sez: I would better use
XmlConvert.ToString( DateTime t )
to encoder and
XmlConvert.ToDateTime( string s )
to decode
'secretGeek' on Tue, 08 Jun 2004 01:04:30 GMT, sez: Thanks mike -- that's much simpler.
here's the original code, rewritten as to use XmlConvert:
(Note that if you don't specify a Format, it will default to use UTC format, "yyyy-MM-ddTHH:mm:sszzzzzz+HH:mm" (where the +HH:mm is the offset from GMT)
Const FORMAT As String = _
"yyyyMMddHHmmss" 'Example format
Dim s1 As String = "20040610140023"
Dim s2 As String
Dim d As Date
'Turn s1 into a date
d = System.Xml.XmlConvert.ToDateTime(s1, FORMAT)
'Turn the date back into a string
s2 = System.Xml.XmlConvert.ToString(d, FORMAT)
'Compare the starting and final strings
Debug.Assert(s1.Equals(s2), _
"The strings s1 and s2 should be equal")
thanks by the way, Mike, for your efforts on the Einstein Quiz. Hope you enjoyed it ;+)
cheers
lb
'Frank' on Tue, 08 Jun 2004 06:18:33 GMT, sez: I'm missing the time zone in this date format, which is not necessarily a problem, just something to keep in mind.
I think documents that can be read by an agent other than ones controlled by yourself AND known to be in the same timezone should always have a timezone.
'Matthew' on Thu, 10 Jun 2004 05:03:36 GMT, sez: See this link from the BCL team:
What is the Recommended Way to Store a DateTime in Text?
http://blogs.msdn.com/bclteam/archive/2004/05/21/136918.aspx
Seeya
Matthew
'Steven' on Thu, 10 Mar 2005 04:36:13 GMT, sez: Any way to compare this to a date in a SQL statement?
'aaa' on Tue, 17 May 2005 05:34:57 GMT, sez: <script>
alert('ok');
</script>
'aaa' on Tue, 17 May 2005 05:36:04 GMT, sez: [table width='100%'][tr][td] (HTML not allowed) [/td][/tr][/table]
'sdfsdfsd' on Tue, 17 May 2005 05:36:38 GMT, sez: <b>fjksdfksdjfk</b>
'sysAdmin' on Tue, 17 May 2005 23:12:58 GMT, sez: The above attempt at an XSS attack is from a very bad web hacker from Thailand, on IP Address 203.144.252.236, possibly using the email address wanvich@hotmail.com.
'http://' on Thu, 06 Apr 2006 09:51:52 GMT, sez: dim d as Date
d.toString("dd/MM/yy")
'JadeMonkey' on Thu, 25 May 2006 18:34:49 GMT, sez: Thanks Guys
'anand' on Wed, 06 Feb 2008 08:18:05 GMT, sez: good
'Ali Asgar' on Sat, 22 Nov 2008 09:29:08 GMT, sez: Wonderful guys.This was really helpfull
|