Convert date time format? - c#

I am trying to convert date time in 12/20/2013 17:40 format to the below format
20 Dec 2013 05:40 pm. How it's possible?

You need to use DateTime.TryParseExact. This should do it
string originalDate = "2/20/2013 17:40";
DateTime parsedDate;
if (DateTime.TryParseExact(originalDate, "M/dd/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate))
{
string requiredFormat = parsedDate.ToString("dd MMM yyyy hh:mm ttt");
}
OutPut:
20 Feb 2013 05:40 PM

mydatetime.ToString("dd MMM yyyy hh:mm tt");

dt.ToString("dd MMM yyyy hh:mm tt");

dateTime.ToString("dd MMM yyyy hh:mm tt");

Method 1: if you have datetime in String format
String str = "12/20/2013 17:40";//20 Dec 2013 05:40 pm
DateTime result;
string date="";
if (DateTime.TryParseExact(str, "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
date=result.ToString("dd MMM yyyy hh:mm tt");
Method 2: if you have datetime in DateTime variable.
String strdatetime=datetime.ToString("dd MMM yyyy hh:mm tt");

DateTime time = DateTime.Now;
Console.WriteLine(time.ToString("dd MMM yyyy hh:mm tt"));

You can use format strings:
this is from 1 minute google:
http://www.csharp-examples.net/string-format-datetime/
http://www.dotnetperls.com/datetime-format

First of all, DateTime doesn't have a format, strings have..
If your 12/20/2013 17:40 is a DateTime, you can use DateTime.ToString(String, IFormatProvider) method to format it like;
date.ToString("dd MMM yyyy hh:mm tt", CultureInfo.InvariantCulture);
If your 12/20/2013 17:40 is a string, then you can use DateTime.ParseExact(String, String, IFormatProvider) method like;
string s = "12/20/2013 17:40";
var date = DateTime.ParseExact(s, "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture);
Console.WriteLine(date.ToString("dd MMM yyyy hh:mm tt", CultureInfo.InvariantCulture));
Output will be;
20 Dec 2013 05:40 PM
Here a demonstration.
For more information, take a look at;
Custom Date and Time Format Strings

In C# 6.0 you can use string interpolation in order to display formatted dates.
DateTime date = DateTime.Parse("12/20/2013 17:40");
string formattedDate = $"{date: dd MMM yyyy hh:mm tt}";

Related

Convert string with long month name to DateTime

I have this string July 1, 2021 9:10 AM and I'm trying to parse it into a DateTime variable.
This isn't working for me. ST is a variable that has the string representation of the date and time.
var Event = DateTime.ParseExact(ST, "MMMM dd, yyyy h:mm tt", CultureInfo.InvariantCulture);
You are using the wrong day format. For a month day without a leading zero, you should use the following:
var Event = DateTime.ParseExact(ST, "MMMM d, yyyy h:mm tt", CultureInfo.InvariantCulture);

Convert dd/MM/yyyy HH:mm tt to MM/dd/yyyy HH:mm tt in C#

I want to convert string as : "25/12/2017 4:00 PM" to "12/25/2017 4:00 PM". My code :
var TDXRSC = "25/12/2017 4:00 PM";
DateTime.ParseExact(TDXRSC, "dd/MM/yyyy hh:mm tt", CultureInfo.InvariantCulture);
But it's not working.
The issue is your date format expected is dd/MM/yyyy hh:mm tt but the reference date only has a single digit hour 4. You are probably better off not expect leading zeros for days, months or hours.
Try..
var TDXRSC = "25/12/2017 4:00 PM";
var input = DateTime.ParseExact(TDXRSC, "dd/MM/yyyy h:mm tt", CultureInfo.InvariantCulture);
This will also still parse 2 digit hours. So var TDXRSC = "25/12/2017 12:00 PM"; will still parse correctly.
var TDXRSC = "25/12/2017 4:00 PM";
var input = DateTime.ParseExact(TDXRSC, "dd/MM/yyyy h:mm tt", CultureInfo.InvariantCulture);
var output = input.ToString("MM/dd/yyyy h:mm tt");
When you call ParseExact you're telling the compiler what format the incoming date is. You can then use ToString() method to provide a format for a string representation of the parsed date.
Hope that .TryParseExtract will be more safe to use for conversion, use like the following:
var dateString = "25/12/2017 4:00 PM";
DateTime inputDate;
if(DateTime.TryParseExact(dateString, "dd/MM/yyyy h:mm tt", CultureInfo.InvariantCulture, DateTimeStyles.None, out inputDate))
{
var output = inputDate.ToString("MM/dd/yyyy hh:mm tt");
Console.WriteLine(output);
}
else
{
Console.WriteLine("Conversion failed");
}
Working Example
var TDXRSC = "25/12/2017 4:00 PM";
DateTime date = Convert.ToDateTime(TDXRSC);
string Format = date.ToString("MM/dd/yyyy h:mm tt");

How to convert string date : "Mon Nov 12 08:00:00 ICT 2012" to "dd/MM/yyyy HH:mm:ss" format in C#

This is my way to convert but it's not work:
string date = "Mon Nov 12 08:00:00 ICT 2012";
DateTime dateConvert =
DateTime.ParseExact(date,
"dd/MM/yyyy HH:mm:ss",
System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine(dateConvert);
so, how to convert it? Thankyou!
Your format string for the DateTime.ParseExact should be ddd MMM dd HH:mm:ss 'ICT' yyyy
See http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx on the details on DateTime format strings.
PARSE EXACT means you're telling it to expect:
dd/MM/yyyy HH:mm:ss
But your input string is not in that format!!!
You need something like:
ddd MMM dd HH:mm:ss \I\C\T yyyy
string date = "Mon Nov 12 08:00:00 ICT 2012";
DateTime d = DateTime.ParseExact(date, "ddd MMM dd HH:mm:ss ICT yyyy", null);
Console.WriteLine(d.ToString("dd/MM/yyyy HH:mm:ss"));
What you've got wrong is that the format you provide to DateTime.ParseExact is supposed to be the format of the date string coming in, not what you want coming out.
Try something like:
string date = "Mon Nov 12 08:00:00 ICT 2012";
DateTime dateConvert = DateTime.ParseExact(date, "ddd MMM dd HH:mm:ss 'ICT' yyyy", null);
Console.WriteLine(dateConvert.ToString("dd/MM/yyyy HH:mm:ss"));
Thanks for all care! This is my solution and it's work cool!
DateTime dateTime = DateTime.ParseExact("Mon Nov 12 08:00:00 ICT 2012", "ddd MMM dd HH:mm:ss ICT yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(dateTime.ToString("MM/dd/yyyy"));

Parsing dates and times from string in many combinations

I need to parse dates and times from strings. The Problem is that the strings can have any possible format. But I also get the format strings.
So i get:
Date = "9/15/2010"
Time = "16:12:45"
DateFormat = "M/dd/yyyy"
TimeFormat = "h:mm:ss"
TimeZone = "+2:00:00" // +/- and time in TimeFormat
But i have some problems parsing these strings.
I can't parse the time
DateTime.ParseExact("16:12:45","h:mm:ss",null,DateTimeStyles.None);
does not work and causes a FormatException. What is wrong with this call?
If the DateFormat contains slashes, i need to escape them #"M\/dd\/yyyy". Are there any other chars that would need escaping?
Can i parse the whole DateTime in one? Somehting like:
DateTime.ParseExact(Date+' '+Time+' '+TimeZone,DateFormat+' '+TimeFormat+' +'+TimeFormat,null,DateTimeStyles.None);
What is wrong with this call?
The "h:mm:ss" format string expects the hours element to be in 12-hour format (h); The hours in your string are in 24-hour format so you need to use H instead:
DateTime.ParseExact("16:12:45", "H:mm:ss", null, DateTimeStyles.None);
Are there any other chars that would need escaping?
Any literal character in your string that clashes with a format specifier will need to be escaped. For example, / is the date separator but \/ means the literal / character; : is the time separator but \: means the literal : character; y is one of the year specifiers but \y is the literal y character.
Can i parse the whole DateTime in one?
Yes.
How about this?
var #return = (DateTime?)null;
if (source != null)
{
source = source.Trim();
if (source.Length > 0)
{
var fs = new string[]
{
"d MMMM yyyy h:mm tt",
"d MMMM yyyy h:mm:ss tt",
"d MMMM yyyy HH:mm",
"d MMMM yyyy HH:mm:ss",
"d MMMM yyyy",
"d/M/yy h:mm tt",
"d/M/yy h:mm:ss tt",
"d/M/yy HH:mm",
"d/M/yy HH:mm:ss",
"d/M/yy",
"d/M/yyyy HH:mm",
"d/M/yyyy HH:mm:ss",
"d/M/yyyy h:mm:ss tt",
"d/M/yyyy",
"d/M/yyyy h:mm tt",
"d-MMMM-yy HH:mm",
"d-MMMM-yyyy h:mm tt",
"d-MMMM-yyyy h:mm:ss tt",
"d-MMMM-yyyy HH:mm",
"d-MMMM-yyyy HH:mm:ss",
"d-MMMM-yyyy",
"d-MMM-yy",
"d-MMM-yy h:mm tt",
"d-MMM-yy h:mm:ss tt",
"d-MMM-yy HH:mm",
"d-MMM-yy HH:mm:ss",
"d-MMM-yyyy",
"d-M-yy h:mm tt",
"d-M-yy h:mm:ss tt",
"d-M-yy HH:mm",
"d-M-yy HH:mm:ss",
"d-M-yy",
"d-M-yyyy",
"yyyy/M/d h:mm tt",
"yyyy/M/d h:mm:ss tt",
"yyyy/M/d HH:mm",
"yyyy/M/d HH:mm:ss",
"yyyy/M/d",
"yyyy-M-d h:mm tt",
"yyyy-M-d h:mm:ss tt",
"yyyy-M-d HH:mm",
"yyyy-M-d HH:mm:ss",
"yyyy-M-d",
"yyyy-MM-ddTHH:mm:ss",
};
#return = DateTime.ParseExact(source, fs,
System.Globalization.CultureInfo.CurrentCulture,
System.Globalization.DateTimeStyles.None);
}
}
return #return;
Do you know which culture the original string belongs to? If you do you can specify a culture specific DateTimeStyle when calling Parse()
DateTime date = DateTime.Parse("<your specific date>", System.Globalization.CultureInfo.GetCultureInfo("<your culture>").DateTimeFormat);
Create a custom DateTimeFormatInfo and pass that into DateTime.Parse. Like this:
string dateValue = string.Format("{0} {1}", "9/15/2010", "16:12:45");
var customDateTimeFormatInfo = new DateTimeFormatInfo();
customDateTimeFormatInfo.FullDateTimePattern = string.Format("{0} {1}", "M/dd/yyyy", "h:mm:ss");
DateTime dt = DateTime.Parse(dateValue, customDateTimeFormatInfo);
No escaping required and handled your "h:mm:ss" fine with no modification required.

C# : How to convert string to DateTime, where the string can have any of the standard datetime format

I had posted a question on DateTime to String conversion, I got many satisfying answers for that .. so I thank StackOverflow very much .. Here is one more problem of String manupulation, I am stuck with .. I have to convert a string (from some external source) using C# code .. the string can have these expected format of DateTime ..
02/31/2009 01:59:59 24 hours format
02/31/2009 01:59:59 AM 12 hours format
2/31/2009 1:59:59
2/31/2009 1:59:59 AM
02/01/2009 01:59:59 AM
2/1/2009 1:59:59
and so on .......
I tried using DateTime(Convert.ToInt32(string_date.Substring(6,4)),Int,Int,Int,Int,Int,Int) ie, By extracting the values of month, Day etcBut it doesn't work .. because I can't extract the values with substring perfectly .. as the length of string is Varying I also have tried to extract the values referring the occurance of "/", "space" and ":" but it becomes bottle neck to derive with (non-)Occurrence of AM/PM Only the length of Day, Month and Hours can vary ..
You can use the DateTime.ParseExact overload that takes a list of formats:
private static string[] formats = new string[]
{
"MM/dd/yyyy HH:mm:ss tt",
"MM/dd/yyyy HH:mm:ss",
"M/dd/yyyy H:mm:ss tt",
"M/dd/yyyy H:mm:ss"
};
private static DateTime ParseDate(string input)
{
return DateTime.ParseExact(input, formats, CultureInfo.InvariantCulture, DateTimeStyles.None);
}
This will throw a FormatException if the passed string does not match any of the given formats. Notice that the formats expecting AM/PM should appear before identical formats without AM/PM ("MM/dd/yyyy HH:mm:ss tt" comes before "MM/dd/yyyy HH:mm:ss").
Update
As Henk points out in the comments, the same functionality is available when using TryParseExact which removes exception situation. Also, paired with nullable types this can be made a bit cleaner:
private static DateTime? ParseDate(string input)
{
DateTime result;
if (DateTime.TryParseExact(input, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
{
return result;
}
return null;
}
Now it will simply return a null reference if it fails to parse the input.
Take a look at the TryParseExact method. Here's an example with the first case:
DateTime date;
// I changed 02/31/2009 to 01/31/2009 because the first is not a valid date
if (DateTime.TryParseExact("01/31/2009 01:59:59", "MM/dd/yyyy HH:mm:ss", null, DateTimeStyles.None, out date))
{
// string successfully parsed => do something with the date
}
You could then keep a list of different formats and try to parse the string with all of them until you succeed.
Here are all the possible formats ..
MM/dd/yyyy 08/22/2006
dddd, dd MMMM yyyy Tuesday, 22
August 2006
dddd, dd MMMM yyyy HH:mm Tuesday,
22 August 2006 06:30
dddd, dd MMMM yyyy hh:mm tt
Tuesday, 22 August 2006 06:30 AM
dddd, dd MMMM yyyy H:mm Tuesday, 22
August 2006 6:30
dddd, dd MMMM yyyy h:mm tt Tuesday,
22 August 2006 6:30 AM
dddd, dd MMMM yyyy HH:mm:ss
Tuesday, 22 August 2006 06:30:07
MM/dd/yyyy HH:mm 08/22/2006 06:30
MM/dd/yyyy hh:mm tt 08/22/2006
06:30 AM
MM/dd/yyyy H:mm 08/22/2006 6:30
MM/dd/yyyy HH:mm:ss 08/22/2006
06:30:07
MMMM dd August 22
yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK
2006-08-22T06:30:07.7199222-04:00
ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
Tue, 22 Aug 2006 06:30:07 GMT
yyyy'-'MM'-'dd'T'HH':'mm':'ss
2006-08-22T06:30:07
HH:mm 06:30
hh:mm tt 06:30 AM
H:mm 6:30
h:mm tt 6:30 AM
HH:mm:ss 06:30:07
yyyy'-'MM'-'dd HH':'mm':'ss'Z'
2006-08-22 06:30:07Z
dddd, dd MMMM yyyy HH:mm:ss
Tuesday, 22 August 2006 06:30:07
yyyy MMMM 2006 August
DateTime dt1 = DateTime.ParseExact("2007/01/01 04:23:12", "yyyy/MM/dd hh:mm:ss",
System.Globalization.CultureInfo.CurrentCulture);
DateTime dt = Convert.ToDateTime("2007/01/01 04:23:12", System.Globalization.CultureInfo.CurrentCulture);
System.Globalization.CultureInfo.CurrentCulture
format param

Categories

Resources