Convert string to Datetime format dd-MMM-yyyy issue - c#

I am getting the account expiry date of the employee from database in a string variable and the values which is in this string variable is in the format : 6/26/2016 9:14:03 AM. Now i want this format to be converted in dd-mmm-yyyy hh:mm:ss am/pm.How can i resolve this.
I tried the following code :
DateTime passexpiredate = DateTime.ParseExact(app_user.GetPasswordExpiry(empCode), "dd-MMM-yyy hh:mm tt", null);
lblPassExpiry.InnerText = "Password Expiry Date: "+passexpiredate.ToString();

Life is to easy using DateTime.ParseExact:
DateTime.ParseExact("12/02/21 10:56:09 AM", "yy/MM/dd HH:mm:ss tt", CultureInfo.InvariantCulture).ToString("dd-MMM-yyyy HH:mm:ss tt");

You've to pass the required date time format in ToString() method as mentioned below:
DateTime passexpiredate = DateTime.Parse(app_user.GetPasswordExpiry(empCode));
lblPassExpiry.InnerText = "Password Expiry Date: " + passexpiredate.ToString("dd-MMM-yyy hh:mm tt");

You need to make DateTime from string 6/26/2016 9:14:03 AM. And then format it to dd-mmm-yyyy hh:mm:ss am/pm

DateTime.ParseExact requires you to specify format exactly as it matches with string representation of datetime value. So that's not going to work for your case.
You will have to first convert string to DateTime instance and then format it while displaying.
string date = "6/26/2016 9:14:03 AM";
var dt = DateTime.Parse(date);
var dtStr = dt.ToString("dd-mmm-yyyy hh:mm:ss tt");
Console.WriteLine(dtStr); // Output: 26-14-2016 09:14:03 AM

Related

How to find the correct custom format for a date-time string

I read date-time strings from a file in 2 different formats:
19/02/2019 08:24:59
2/17/2019 12:25:46 PM
For the first format the custom format string I wrote is:
string firstDate = "19/02/2019 08:24:59";
string customFormatForFirstDateTimeString = "dd/mm/yyyy hh:mm:ss";
and I use it as follows:
string firstResultingDateAndTime;
bool parsingSuccessful = DateTime.TryParseExact(
firstDate,
customFormatForFirstDateTimeString,
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out firstResultingDateAndTime);
The problem is that parsingSuccessful results false.
For the second date-time string, the code is as follows:
string secondDate = "2/17/2019 12:25:46 PM";
string customFormatForSecondDateTimeString = "m/dd/yyy hh:mm:ss PM";
string secondResultingDateAndTime;
parsingSuccessful = DateTime.TryParseExact(
secondDate,
customFormatForSecondDateTimeString,
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out secondResultingDateAndTime);
Also here I receive
parsingSuccessful == false;
I reckon that the custom format strings do not fit the date-time strings, but I was not able to figure out why.
Please help.
Thank you in advance.
Well, mm stands for minutes, not months (we have MM for it) that's why dd/mm/yyyy format should be dd/MM/yyyy.
Another issue with hour format where we have hh for 0..12 range (with tt for AM/PM) and HH for 0..23 interval:
string firstDate = "19/02/2019 08:24:59";
// Since we don't have AM / PM we can conclude that hour is in 0..23 range
string customFormatForFirstDateTimeString = "dd/MM/yyyy HH:mm:ss";
string secondDate = "2/17/2019 12:25:46 PM";
string customFormatForSecondDateTimeString = "M/dd/yyyy hh:mm:ss tt";

How to Convert a string "dd/mm/yyyy HH:MM:SS" to "HH:MM" for on-line Test Duration?

Code :-
public string TestDuration { get; set; }
TestDuration =DateTime.Parse(dr.TestDuration.ToString()).ToShortTimeString() ;
I have a string of the format dd/mm/yyyy HH:MM:SS am/pm, it display date and time in my view, I need hours and minutes for online test duration in format hh:mm.
Example: testduration= 2hr or 2:30 mins
i have a string of type DateTime
That's not possible. You either have string or have DateTime. Your variable type can't be both of them.
You can format your DateTime with HH:mm format like;
var result = DateTime.Parse(dr.TestDuration.ToString())
.ToString("hh:mm", CultureInfo.InvariantCulture);
You didn't mentioned it but, on the other hand, your dr.TestDuration already might be a DateTime. You might wanna format this value instead to generate it's string representation, parsing it to DateTime and generate it's string representation again.
var result = dr.TestDuration.ToString("hh:mm", CultureInfo.InvariantCulture);
After your edit:
Since your TestDuration string and it has "dd/MM/yyyy HH:mm:ss tt" format, you need to parse it to DateTime and generate it's string representation with hh:mm format.
TestDuration = DateTime.ParseExact(dr.TestDuration, "dd/MM/yyyy HH:mm:ss tt",
CultureInfo.InvariantCulture)
.ToString("hh:mm", CultureInfo.InvariantCulture);

C# date time format issue need data dd/mm/yy hh:mm

I want to display date in this format dd/mm/yy hh:mm
HrCreatedDate = DateTime.ParseExact(objMessagePoco.CreatedDate.ToString(), "dd/mm/yyyy hh:mm", CultureInfo.InvariantCulture);
But it's giving me an error:
String was not recognized as a valid DateTime.
Why is that?
DateTime.ParseExact parses a string into a DateTime. If you just want to format an existing DateTime as a string, just pass the format to ToString:
var myStr = objMessagePoco.CreatedDate.ToString("dd/MM/yy HH:mm");
I believe the issue was you needed uppercase MM for month.

Parsing a string to DateTime, formatting, then back to string - C#

I'm pulling a date/time from a MS SQL Server 2008 db and trying to format the date to show just the date in "dd/MM/yyyy" format.
The data in the DB looks like this:
2011-05-04 15:50:00.000
The unformatted string when displayed appears as this:
5/25/2011 8:47:00 AM
Yet this code fails when I try to parse it to the correct format:
DateTime dateA = DateTime.ParseExact(curShopDate, "ddMMyyyy", System.Globalization.CultureInfo.InvariantCulture);
curShopDate = dateA.ToString();
I also tried this code, trying to split just the date portion away from the time:
string[] stringA = curShopDate.Split(' ');
DateTime dateA = DateTime.ParseExact(stringA[0], "ddMMyyyy", System.Globalization.CultureInfo.InvariantCulture);
curShopDate = dateA.ToString();
Both versions crashed with an "String was not recognized as a valid DateTime." error.
The issue is with your format parameter. Your string is not in the ddMMyyyy format, it's in the M/dd/yyyy format:
string curShopDate = "5/25/2011 8:47:00 AM";
DateTime dateA = DateTime.ParseExact(curShopDate.Split(' ')[0], "M/dd/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
You could also parse the string without stripping the time from the date:
string curShopDate = "5/25/2011 8:47:00 AM";
DateTime dateA = DateTime.ParseExact(curShopDate, "M/dd/yyyy h:mm:ss tt",
System.Globalization.CultureInfo.InvariantCulture);
According to MSDN:
The DateTime.ParseExact(String, String, IFormatProvider) method parses
the string representation of a date, which must be in the format
defined by the format parameter. It also requires that the and
elements of the string representation of a date and time appear
in the order specified by format, and that s have no white space other
than that permitted by format.
So, if I'm reading that correctly, you specified the format as "ddMMyyyy" but your string is in "M/dd/yyyy h:mm:ss tt". Try either changing your format to "M/dd/yyyy h:mm:ss tt" or switch to DateTime.TryParse().
If you use ParseExact, then you must specify the exact format: "M/d/yyyy h:mm:ss tt", which matches your date string "5/25/2011 8:47:00 AM".
You can take the date component of a date/time with:
DateTime dateTime = DateTime.Now;
DateTime dateOnly = dateTime.Date;

Problem with date type

i receive this date: 9/20/2010 3:32:32 PM
i need to convert to datetime.
i try:
DateTime DateFrom = DateTime.ParseExact("9/20/2010 3:32:32 PM", "dd/M/yyyy", CultureInfo.InvariantCulture);
but i get error: String was not recognized as a valid DateTime.
in my computer the region is: Hebrew (Israel) dd/MM/yyyy for short date and hh:mm for short time
how to fix it ?
thank's in advance
If you're receiving "9/20/2010 3:32:32 PM" as a string, then trying to parse it as if it were in the "dd/MM/yyyy" format is clearly wrong - that's try to use a month of 20. You're also only parsing part of the string - you need to either trim your string or provide the complete format.
Try this:
DateTime dateFrom = DateTime.ParseExact("9/20/2010 3:32:32 PM",
"M/dd/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture);
Note that using this sort of strict parsing will only work if you can guarantee that that will always be the format. Where are you getting this data from?
How could it work man.You are converting into "dd/MM/yyyy " & putting month as 20.In your
question dd/M/yyyy is wrong.It will like dd/MM/yyyy.
By default format is MM/DD/yyyy.
simple way to do .......
DateTime DateFrom = DateTime.Parse("9/20/2010 3:32:32 PM");
if you want to provide a specific Format so use like that
DateTime DateFrom = DateTime.ParseExact("20/09/2010 3:32:32 PM", "dd/MM/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
I hope it works.
It looks like your original date string is in a US format (i.e. m/dd/yyyy). Try replacing your third parameter with new CultureInfo("en-US")
DateTime dateFrom = DateTime.ParseExact("9/20/2010 3:32:32 PM", "M/dd/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
Works for me
I wouldn't use ParseExact() when I know that time string is formatted by invariant culture.
DateTime dateFrom = DateTime.Parse(dateString, CultureInfo.InvariantCulture);
is both more compact and more clear.

Categories