I am writing an application in C#. I have a DateTime object which I need to output in a format specified by customer input. The problem is that the format string that the user provides is not in c# jargon, the user is entering the strftime format (i.e.:%Y %m %d) , so I need to convert that to c# format. In this particular example the c# format for "%Y %m %d" will be "yyyy MM dd" .So before I write my own parser, isn't there already any function that will help map these two formats?
strftime datetime formats vs c# date time formats
I have seen this question asked before, but people did not understand what the question was (Convert Date Formatting Codes to date), so fingers cross!
Many thanks
Related
I want to know if a particular string is a valid date or not while the date format can be any possible one.
(NOTE- date format is unknown).
Currently I am using
DateTime.TryParse, but its not working with all the dates as it is not parsing this date string=> 5-27-58.
Cant use DateTime.ParseExact it requires a specific date formates.
what is the best approach to do this.
I've created a webtest and have a CSV data source that contains a column with a list of short dates (MM/dd/yyyy)
I need to manipulate the parameter due to part of the web page I'm testing has a form parameter that needs it to be formatted as yyyyMMdd
When the date that is captured from the data source (ex: 02/12/2016), I noticed in the Context tab of my test run that the format to "2/12/2016 12:00:00 AM"
I've created a Request plug-in and added the following code:
public override void PreRequest(object sender, PreRequestEventArgs e)
{
base.PreRequest(sender e)
string CSVDate = e.WebTest.Context["<datasource date column>"].ToString();
DateTime dt = DateTime.ParseExact(CSVDate, "MM/dd/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
e.WebTest.Context.Add("NewDate", dt.ToString("yyyyMMdd"));
}
This generates a String was not recognized as a valid DateTime error. I tried changing the format to MM/dd/yyyy, but I encountered the same error.
Does anyone know how the correct DateTime format I should be using?
The date-time as shown in the context is 2/12/2016 12:00:00 AM. This has only one digit for the month whereas the format specifier has MM which wants two digits. The date-time also contains the letters AM that are not matched by the format.
Modifying the format to be M/dd/yyyy HH:mm:ss matches the date-time 2/12/2016 12:00:00, but does not match the AM part. In theory the tt format specifier should match this, but it did not work for me.
Rather than using ParseExact you can use Parse and it works out the correct format. Using the following worked on the date-time string provided:
DateTime dt1 = DateTime.Parse(CSVDate, new System.Globalization.CultureInfo("en-US"));
The CultureInfo is needed because the input date has the month and the days the wrong way around.
However, the real problem is in the way CSV files are handled by the web test. It appears to read them using the same logic as Microsoft Excel uses when reading CSVs. Namely, if it looks like a date then convert it to a date. So any string matching dd/dd/dddd (where d is a digit) might be connverted to a date. (E.g. 15/15/2017 will not be converted because 15 is not a month number.) I recommend rewriting the CSV to format the input date differently, use something that Excel would not treat as a date. One option is to have the date in three columns of the CSV, so have explicit day,monthandyearcolumns. Another option is to add non-date characters to the string and format it correctly, eg asz20160212and then remove thezwithin the web test. Generally, I would advise to avoid the conversion of string toDateTime` then another conversion to a different string.
I have a textbox with this mask: year/ month /day hour :min
The datetime format is Persian like 1392/12/11 12:43
I need to convert this string to standard English format so I used pesiancalender class.
As you can see the function todate() expects the values separately, I don't know how can I separate the string to this values! I mean I don't know how can I detect year and month and day and hour and min in string.
You can use either DateTime.TryParseExact method with providing the date format to it with culture info about persian date.
Edit: as I found out:
Currently, the PersianCalendar class is not an optional calendar for any culture supported by the CultureInfo class and consequently cannot be a default calendar.
So, you can't use the approach I've suggested. Some investigation led me to the this project for working with Persian date time and some hacks for the CultureInfo.
Such questions were already on SO, so I suggest to use their approach, and to write some helper class to solve your problem.
I have searched stackoverflow for an answer but no luck. I am developing a windows application and I have some strings in different date formats,
eg.
dd/MM/yyyy
MM/dd/yyyy
MM-dd-yyyy
dd-MM-yyyy
dd/MM/yyyy hh:mm::ss
MM/dd/yyyy hh:mm::ss
etc...
But I need to convert in to a common format - dd/MM/yyyy. The application can run in any windows machines in different culture.
What is the correct way to do it?
EDIT: One more thing I may not know what the format of incoming string.
Thanks in advance.
Use DateTime.ParseExact with the different patterns as formats.
If after parsing you really need to use a string representation, use the ToString method of the DateTime with the explicit format that you're interested in (so that it is culture-invariant). It's better however to keep the DateTime because this is format-agnostic.
You could distinguish between those formats that use different separators (i.e. "/" vs "-"). But how would you know if date such as 10/11/2010 represents 10th of November or 11th of October? If one number is not bigger than 12, there is no reliable way to do this without knowing an exact format.
As others have pointed out, if you do know the exact format, then you can use DateTime.ParseExact.
If you are processing some import file with a lot of dates in the same unknown format, you could try different formats and hope there is exactly one that doesn't give format errors.
Or to put it another way: split the "dates" into three numbers and check the range of values for each of those numbers. Values > 1900 will be years. If you find values from 1 to 31, those will be days.
Values from 1 to 12 might be months, but could also be days. Try and identify each of the parts.
The best way is to ask the supplier of those dates for the format.
To run this program on different culture, i think you should creat a function to indentify the culture of this string format and then use Datetime.Parse
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
In C#, given a DateTime object, how do I get a ISO 8601 date in string format?
I've a variable with type DateTime in my C# code.
How do I convert this to "Z" format?
Thanks.
Do you mean converting to a string time/date like 2009-06-15 20:45:30Z? If so then try:
mydate.ToUniversalTime().ToString("u");
See http://msdn.microsoft.com/en-us/library/az4se3k1.aspx for info on Standard Date and Time format strings. In particular read the entry on using the "Universal Sortable ("u") Format Specifier" whcih explains why I have the ToUniversalTime call in there.
If you mean the universal sortable date/time pattern:
string formatted = theDate.ToString("u");
Example result:
2009-06-15 20:45:30Z
Based on a quick search it seems to me that you simply mean UTC time, in which case you can use the ToUniversalTime method of DateTime, and when displaying as a string append "Z" to the end, or use the Universal Format Specifier when calling ToString, or you could construct it using a number of format specifiers in the correct order.
I guess your format is ISO 8601: DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ")
Maybe a duplicate of this topic: Given a DateTime object, how do I get an ISO 8601 date in string format?