I am getting data from XML in format of MM/dd/yyyy HH:mm:ss tt (e.g 7/21/2016 5:43:03 PM) but I want to convert it into date format of only dd/MM/yyyy (e.g 21/7/2016)
            Asked
            
        
        
            Active
            
        
            Viewed 2,105 times
        
    3 Answers
3
            You could try something like this:
var dt = DateTime.ParseExact(input, "M/dd/yyyy HH:mm:ss tt", CultureInfo.InvariantCulture)
                 .ToString("dd/MM/yyyy");
Initially, using the ParseExact you create a DateTime object based on your input and later using the ToString you create the string representation of your DateTime in the specified format.
For info about the DateTime.ParseExact have a look here.
 
    
    
        Christos
        
- 53,228
- 8
- 76
- 108
- 
                    my xml date is '6/30/2016 11:45:32 AM ' and my code to convert is DateTime.ParseExact(date, "MM/dd/yyyy HH:mm:ss tt", CultureInfo.InvariantCulture).ToString("dd-MM-yyyy") but still i am getting error {"String was not recognized as a valid DateTime."} – Luqman Jul 25 '16 at 07:09
1
            
            
        You can do this as a quick trick:
string date = "7/21/2016 5:43:03 PM";
var dateOnly = DateTime.Parse(date).ToString("MM/dd/yyyy");
Explanation:
DateTime.Parse(date)    <--- Converts the string to DateTime object.
ToString("MM/dd/yyyy")  <--- Converts the DateTime Object to the specified format.
 
    
    
        Zein Makki
        
- 29,485
- 6
- 52
- 63
0
            
            
        string date = "7/21/2016 5:43:03 PM";//Date from XML
DateTime dateTime;
if (DateTime.TryParse(date, out dateTime))
{
    var dateOnly = dateTime.ToString("dddd, MMMM dd, yyyy");
}   
 
    
    
        vivek kv
        
- 406
- 6
- 11
