Convert String to DateTime Object
-
Monday, July 31, 2006 2:24 PM
Hi All
I am new to this forum...
I am storing string "21-07-2006" (21 July 2006) in a string variable and want to store this value in a DateTime Object. As it takes value in format #MM-dd-yyyy# i have to convert this value into this format.
I have tried this
string Objdate = "21-07-2006";
Response.Write(Convert.ToDateTime(Objdate ).ToString("mm-dd-yyyy").ToString());But It generate Errors "String is not n correct date time format" ...something..2
Pls help
Sachin
All Replies
-
Monday, July 31, 2006 2:41 PM
Try this...
string strDate = "21-07-2006";
DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
dtfi.ShortDatePattern = "dd-MM-yyyy";
dtfi.DateSeparator = "-";
DateTime objDate = Convert.ToDateTime(strDate, dtfi);
MessageBox.Show(objDate.ToShortDateString()); -
Monday, July 31, 2006 2:45 PM
try this:
string myDate = "21-07-2006";
DateTime myDateTime = DateTime.Parse(myDate);
Response.Write(myDateTime.ToString("mm-dd-yyyy"));
does this help?
-
Tuesday, August 08, 2006 8:12 AM
First...Thanks for reply...but error is the same on the line...
string myDate = "21-07-2006";
DateTime myDateTime = DateTime.Parse(myDate); "string was not recognised as a valid DateTime."
Response.Write(myDateTime.ToString("mm-dd-yyyy"));
-
Tuesday, August 08, 2006 8:17 AM
Thanks Dear...
It Works....Can I mention the datePattern in string format like
DateTime objDate = Convert.ToDateTime(strDate, "dd-MM-yyyy");
Like in Vb.Net
-
Tuesday, August 08, 2006 12:29 PM
looks like perhaps a culture problem.
IFormatProvider theCultureInfo = new System.Globalization.CultureInfo("en-GB", true); DateTime theDateTime = DateTime.ParseExact(abc, "mm-dd-yyyy", theCultureInfo);Does this help? of course change the cultureinfo appropriately
-
Sunday, August 13, 2006 6:24 PM
Thanks for help...It Works
I have one more queries regarding Deserialize Classes that inherits from DictionaryBase Class
I have Customer Class that Contain ContactCollection Class and Occupation Class that again have ContactCollection Class
ContactCollection Class inherits from DictionaryBase Class
The Structure are as follows
public class Customer{
public Customer(){
}
private string _FirstName=null; private string _LastName = null;private
ContactCollection _ContactColl = new ContactCollection(); public string FirstName{
get { return _FirstName; } set { _FirstName = value;}}
public string LastName{
get { return _LastName; } set { _LastName = value; }}
public ContactCollection ContactColl{
get { return _ContactColl; } set { _ContactColl = value; }}
}
public
class Contact{
public Contact(){ }
private string _ContactFirstName = null; private string _ContactLastName = null; public string ContactFirstName{
get { return _ContactFirstName; } set { _ContactFirstName = value; }}
public string ContactLastName{
get { return _ContactLastName; } set { _ContactLastName = value; }}
}
public class ContactCollection : System.Collections.DictionaryBase, IXmlSerializable{
public XmlSerializer _XmlSerializer = null;#region
Public Properties //public ICollection Keys //{ // get // { // return (Dictionary[Keys]); // } //} ///// <summary> ///// ///// </summary> //public ICollection Values //{ // get // { // return (Dictionary.Values); // } //}#endregion
#region
Public Methods /// <summary> /// /// </summary> /// <param name="key"></param> /// <param name="value"></param> public void Add(String key, Contact value){
Dictionary.Add(key, value);
}
public bool Contains(String key){
return (Dictionary.Contains(key));}
public void Remove(String key){
Dictionary.Remove(key);
}
#endregion
#region
Protected Override Method protected override void OnInsert(Object key, Object value){
if (key.GetType() != Type.GetType("System.String")) throw new ArgumentException("key must be of type String.", "key");
if (value.GetType() != Type.GetType("Test.Contact")) throw new ArgumentException("value must be of type of nucleus.wto.core.DocumentImage.", "value");}
protected override void OnRemove(Object key, Object value){
if (key.GetType() != Type.GetType("System.String")) throw new ArgumentException("key must be of type String.", "key");}
protected override void OnSet(Object key, Object oldValue, Object newValue){
if (key.GetType() != Type.GetType("System.String")) throw new ArgumentException("key must be of type String.", "key"); if (newValue.GetType() != Type.GetType("Test.Contact")) throw new ArgumentException("newValue must be of type nucleus.wto.core.DocumentImage.", "newValue");}
protected override void OnValidate(Object key, Object value){
if (key.GetType() != Type.GetType("System.String")) throw new ArgumentException("key must be of type String.", "key"); if (value.GetType() != Type.GetType("Test.Contact")) throw new ArgumentException("value must be of type nucleus.wto.core.DocumentImage.", "value");}
#endregion
#region
IXmlSerializable Members public System.Xml.Schema.XmlSchema GetSchema(){
return null;}
public XmlSerializer Serializer{
get{
if (_XmlSerializer == null)_XmlSerializer =
new XmlSerializer(typeof(Contact)); return _XmlSerializer;}
}
public void ReadXml(XmlReader reader){
try{
reader.Read();
while (reader.NodeType != XmlNodeType.EndElement){
Contact ObjContact;ObjContact = (
Contact)Serializer.Deserialize(reader);reader.MoveToContent();
this.Dictionary.Add("1", ObjContact);}
}
catch (Exception ex){
throw; }}
public void WriteXml(XmlWriter writer){
try{
foreach (string Keys in this.Dictionary.Keys){
Serializer.Serialize(writer,
this.Dictionary[Keys]);}
}
catch (Exception ex){
throw; }}
#endregion
}
XmlSerializer Xsr = new XmlSerializer(typeof(Customer)); MemoryStream Ms = new MemoryStream();Xsr.Serialize(Ms, ParObj);
Ms.Position = 0;
StreamReader Sr = new StreamReader(Ms); String XmlStr = Sr.ReadToEnd(); StringReader StrR = new StringReader(XmlStr); object ObjPar = Xsr.Deserialize(StrR);Now the Problems comes in Deserialization of Customer Class Object. If I am having more than one object of Collection Classes like
AddressInfo Class, Expenses Class It Deserialize only one object not others...I thing there is some exception occured
Pks help
-
Sunday, August 13, 2006 11:40 PM
Fine, string to DateTime can be handled by Convert class. Look the following code snippet:
<code>
// Display the value and Kind property of a DateTime structure, the
// DateTime structure converted to local time, and the DateTime
// structure converted to universal time.
public static string datePatt = @"M/d/yyyy hh:mm:ss tt";
public static void Display(string title, DateTime inputDt)
{
DateTime dispDt = inputDt;
string dtString;
// Display the original DateTime.
dtString = dispDt.ToString(datePatt);
Console.WriteLine("{0} {1}, Kind = {2}",
title, dtString, dispDt.Kind);
// Convert inputDt to local time and display the result.
// If inputDt.Kind is DateTimeKind.Utc, the conversion is performed.
// If inputDt.Kind is DateTimeKind.Local, the conversion is not performed.
// If inputDt.Kind is DateTimeKind.Unspecified, the conversion is
// performed as if inputDt was universal time.
dispDt = inputDt.ToLocalTime();
dtString = dispDt.ToString(datePatt);
Console.WriteLine(" ToLocalTime: {0}, Kind = {1}",
dtString, dispDt.Kind);....
</code>
and tell me, when to use or not to use at sign '@' before 'datePatt' string to literise a string for CLR ???
@ to be :(
-
Sunday, July 26, 2009 1:46 PMTry This
string givendate = "27/07/2009";
IFormatProvider iFP = new System.Globalization.CultureInfo("en-GB", true); //This does the trick FROM US to Great Britain
Convert.ToDateTime(givendate + " 2:00:00 PM", iFP); -
Thursday, October 15, 2009 4:59 AMThank you very much.
But I convert in vb.net
And I can't convert to show in Datetimepicker
How to do? -
Sunday, September 19, 2010 11:15 PM
Unspecified culture
string strDate = "19/09/2010 14:00:00"; var dtfi = new DateTimeFormatInfo { ShortDatePattern = "dd-MM-yyyy HH:mm:ss", DateSeparator = "-" }; DateTime objDate = Convert.ToDateTime(strDate, dtfi);
Programer -
Friday, April 29, 2011 8:02 AM
Thnx I got the solution from your given code.
string strDate = "04-22-2011";
DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
dtfi.ShortDatePattern = "MM-dd-yyyy";
dtfi.DateSeparator = "-";
DateTime objDate = Convert.ToDateTime(strDate, dtfi);
-
Friday, August 24, 2012 10:39 AM
sir ...
i want Convert from MM/dd/yyyy hh:mm:ss tt to dd/MM/yyyy hh:mm:ss tt
only return MM/dd/yyyy
could plz any one help me!
this s sample code for mine
string enteredVal = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt");
DateTime dt = DateTime.ParseExact(enteredVal, "dd/MM/yyyy hh:mm:ss tt", CultureInfo.CreateSpecificCulture("en-US"));
MessageBox.Show(dt.ToString());
Thanks & Regards,
- Edited by Brinda Arumugam Friday, August 24, 2012 10:40 AM
-
Wednesday, August 29, 2012 11:59 AM
Hi Brinda,
Use
DateTime.Now.ToShortDateString("dd/MM/yyyy")Happy Coding.
Regards,
Jaliya
Jaliya Udagedara(MCPD) http://www.jaliyaudagedara.blogspot.com

