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.
Related
Basically,I am reading excel file where one of that columns has date format like : dd/MM/yyyy eg: 11/04/2016
When I am using DateTime.TryParse() to parse that string into datetime method TryParse() treated first numbers like month (number 11 in example above). However the same code running on the other computers will take the second number (04 in example above) as the month.
So my question is why there is a difference between them, what actually decide the behavior of TryParse method?
I think the main difference is in IFormatProvider (hard to say if I can't check some settings in target system), but I usually use other method to get proper DateTime object:
DateTime someDate = DateTime.ParseExact(myStringDate, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
It always gives me what I want no matter how client environment is configured.
Hope this helps. :)
From DateTime.TryParse(String, DateTime) documentation:
Because the DateTime.TryParse(String, DateTime) method tries to parse
the string representation of a date and time using the formatting
rules of the current culture, trying to parse a particular string
across different cultures can either fail or return different results.
If a specific date and time format will be parsed across different
locales, use the
DateTime.TryParse(String, IFormatProvider, DateTimeStyles, DateTime)
method or one of the overloads of the TryParseExact method and provide
a format specifier.
That means your computers have different culture settings which is pointed in CurrentCulture property.
Looks like one computer's current culture have dd/MM/yyyy and the other computer's current culture have MM/dd/yyyy as a standard date and time format.
Since you are sure your values are always in dd/MM/yyyy format, I would use DateTime.ParseExact instead of Datetime.TryParse or DateTime.TryParseExact methods like;
var dt = DateTime.ParseExact(yourColumnValue, "dd/MM/yyyy", CultureInfo.InvariantCulture);
Or you can sets all computers current culture to like the first computer but remember, CultureInfo data is not a stable data that might be change in future with a windows update, .NET Framework version or OS version.
I would like to know if there is a way to get the current date in the following format :
YYYY.MM.DD
I know that I can get the current date, and then manipulate it to get the desired format, but I was wondering if there is a pattern solution to specify my own format and so directly getting a string like this :
"2014.06.10"
string dateFormatted = DateTime.Today.ToString("yyyy.MM.dd");
You can use the string.Format() method and reference this link http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
The {#} represents the argument being passed and the {#:yyyy} is a way of formatting to grab just the year in format of yyyy. Same for MM and dd. Worth noting is that mm is minutes and MM is months which may be confusing as day and year are also lower case.
string.Format("{0:yyyy}.{0:MM}.{0:dd}", DateTime.Now);
So this seems like an easy thing to do, but still has me stumped. I want to display a list of strings to my user, based off of a few file's creation dates. So basically, display a list of DateTimes. The challenge is that want to use a custom format (something like 5/6/13 12:01 PM) but I want he date part of that to display differently based on how you have your system displaying the date (ie. a Brit would display that date as 6/5/13).
I thought I could just build two strings (one for date and one for time) and make sure that they date is region-formatted, but there is no default option for 5/6/13 (only 5/6/2013):
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx
Next I hoped maybe the DateTime.ToShortDateString() function would work, but it displays as 5/6/2013 as well.
I know I can use a completely custom format like this: DateTime.ToString("M/d/yy h:mm tt") but I don't want to fix the date with the month before the day.
I suppose if I can' figure anything out then I could just build a custom datetime for America and for Europe and then query the OS for what datetime they are displaying in. But that seems really excessive. Any thoughts?
You could retrieve the current ShortDate format from current culture, change it and use it with ToString()
var currentDate = DateTime.Now;
var shortDateFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
var newShortDateFormat = shortDateFormat.Replace("yyyy", "yy");
Console.WriteLine(currentDate.ToString(shortDateFormat));
Console.WriteLine(currentDate.ToString(newShortDateFormat));
System.Globalization.CultureInfo implements IFormatProvider, so you can provide a CultureInfo object as a parameter to the ToString method.
MSDN seems to have an example of exactly what you want.
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
I have date in this format "1999-05-31T13:20:00.000-05:00" I want to add some hours or days to it . BizTalk expects same datetime format how can i pass it as Datetime in that format ? No string . Date time with same format as source date.
You should be able to do something like this to get it into a DateTimeOffset object. After that you can call whatever methods you want on it.
DateTimeOffset dateTime = DateTimeOffset.Parse( "1999-05-31T13:20:00.000-05:00" );
To get the value back just use a formatting string.
dateTime.ToString( "O" ); //this should be the same format as you started with
Here are some other options http://msdn.microsoft.com/en-us/library/az4se3k1.aspx
Here is a link to the DateTimeOffset structure
http://msdn.microsoft.com/en-us/library/bb351654.aspx
I hope this helps.
The DateTime object is format-independent (for the most part). So whether or not it starts in the format you list or not doesn't matter. You can always get it back into that format (using the ToString("o") function). But that's as a string (when format matters).
After a quick search, it looks like you must be talking about string format, even though you said no string. So the other answer or the ToString("o"); part of mine is relevant.