Change datetime format from one to another programmatically - parsing

In my program, I need to change the input datetime information from Turkish (d.M.yyyy) language to English (M/dd/yyyy).
When client edits datetime information in Turkish datetime format(For Example: 24.9.2015), i need to convert this information in English datetime format(Like; 9/24/2015).
I'm trying to parse the input, but not convert the correct format that i need, code block as in the following.
DateTime.TryParseExact(InputDate,"d.M.yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
I need the output named "result" in English U.S datetime format. I also tried to change the code as CultureInfo(en-US) but it doesn't work.
Any ideas?

In what language are you trying to achieve this?
Assuming it is c#, DateTime does not have a specific format until you try to convert it into a string and you can convert it to US version by specifying the correct format when calling ToString()
string americanFormat = myDate.ToString("MM/dd/yyyy");

Related

Filter Data between two Persian date in c#

Hi I want to filter my data which is between two Persian date.
Note: Persian date is a string data type like : "1400/02/23"
when I want to filter my table in SQL Server I simply write like the code bellow:
SELECT * FROM Table
WHERE Date >="1400/01/02" AND Date <="1400/05/10"
but in C# syntax I do not know how to fetch date between two string to use in my filter back-end code. If I simply compare the error raise that string type data can not use comparison operator.
I would be glad and grateful if somebody help me
First, convert Persian Date to Gregorian date and then do the comparison.

How to pass custom date format to CsvProvider in F#

I'm using CsvProvider to parse a CSV file i receive from an external source. The date format of the file is dd/MM/yy. But CsvProvider infers it as MM/dd/yy and get all dates wrong.
Is there any way I can pass the dateformat to CsvProvider?
I can read the dates as string and then convert them using Seq.Map to Datetime (which is two steps).
I'm trying to find out if i can do in a single step
Example
csv file sample.csv is having data like below
11/01/90
12/01/90
13/01/90
14/01/90
15/01/90
I'm using the below F# code
type MyCsvProvider = CsvProvider<Sample= "sample.csv", HasHeaders=false, Schema="Date (date)">
MyCsvProvider.Load("sample.csv")
The CsvProvider type provider does not have a way of explicitly specifying a format for parsing dates, so if you have some completely non-standard format, you'll just have to read it as string (which is what CSV provider infers if it cannot parse dates automatically) and then parse the date values explicitly.
That said, you can specify the Culture parameter, which makes it possible to parse dates in format that is common in countries outside of the US. For example, your date format would work fine with the en-GB culture (in the UK, you first write day, then month and then year).
In the following small example, the Test property is inferred as DateTime:
type A = CsvProvider<"""Test
11/01/90
12/01/90
13/01/90
14/01/90
15/01/90""",Culture="en-GB">
let r = A.GetSample().Rows |> Seq.head
r.Test

Need conversion technique from Times ten dateTime to Saxon Timestamp

Trying to retrieve a dateTime Information from Timesten Database and use it in Saxon Xquery .below is the example for that and getting the below error . Do we need to convert timesten dateTime to saxom dataTime if yes how to do that ? pls help me if have idea.
let $DateVar:=fn:data($PERSON/BIRTHDAY)
where as $PERSON/IN_BIRTHDAY is 2010-04-04 03:16:04.000000
if I am trying
let $day-b-DT :=day-from-dateTime($DateVar)
I am getting
Validation error
FORG0001: Invalid dateTime value "2010-04-04 03:16:04.000000" (Day must be two digits)
net.sf.saxon.s9api.SaxonApiUncheckedException: Invalid dateTime value "2010-04-04 03:16:04.000000" (Day must be two digits)
I believe the problem is just your string format, which should be
"2010-04-04T03:16:04.000000". See the documentation for dateTime for more information.
I don't know anything about the Times ten database, or whether you retrieve values in a "rich" format which you happen to be formatting to a string (in which case you should be able to specify a different format) but I believe that's what's wrong.

How to compare dates independent of current culture in .NET

I hardcode a trial expiration date in my .net 2.5 app. how do I compare it with the user's system date such that the comparison is accurate regardless of the user's culture settings?
DateTime maxTrialDate = DateTime.Parse("11/17/2020", new System.Globalization.CultureInfo("en-US"));
DateTime curDate = DateTime.Parse(DateTime.Now.ToShortDateString(), new System.Globalization.CultureInfo("en-US"));
//the next line of code uses the DateDiff method to compare the two dates -dont recall its //exact syntax.
On my XP machine the above works if the control panel regional setting for datetime is en-US, but if I change it to en-AU, then the above code that sets curDate fires a FormatException "Date is not in a correct string format"
If you avoid using strings to represent the dates, you will not encounter this problem:
DateTime maxTrialDate = new DateTime(2020, 11, 17);
if (DateTime.Now.Date > maxTrialDate)
{
// expired
}
The DateTime is created by explicitly defining the day, month and year components, so the regional settings will not confuse matters.
What about just using CultureInfo.InvariantCulture all over the place?
You can use System.Globalization.CultureInfo.InvariantCulture
If I remember correctly, in most places outside the US, the standard date format is dd/mm/yyyy, rather than the US standard of mm/dd/yyyy. It might be that when trying to parse the date, it believes the 17 is the month, which is an invalid month, thus causing the error.
Why are you using the Parse method if you are hardcoding expiration date just compare it to
DateTime.now
The FormatException is expected since you explicitly ask the parser to use en-US.
Try calling the one-argument overload of DateTime.Parse(), or alternatively, if you really want to use the two-args overload (*cough*FxCop*cough*), something like:
using System.Globalization;
DateTime.Parse("11/17/2020", CultureInfo.CurrentCulture);

How do I pass a date via the URL, for my Action to read in MVC?

How do I pass a date via the URL?
Here is what I am trying to do, as as you'll see the date uses forward slashes which are invalid in the URL
http://localhost/Controller/Action/id=0d5375aa-6d43-42f1-91f0-ea73d9beb361&date=02/12/2009
The ISO 8601 standard is yyyy-MM-dd, which is unambiguous and does not contain any invalid URL characters, and works fine in DateTime.Parse/TryParse.
Another option is to use whatever format you want and simply encode the URL using HttpServerUtility.UrlEncode/UrlDecode.
You could pass a date in the query string using a specific format, say yyyymmdd and then parse it correctly in your Controller.
&date=02/12/2009
change to
&date=20091202 (yyyymmdd)
You could either create a wrapper around the DateTime object that was instantiated using this new format or just parse it yourself in the Controller.
public MyWrapperDate(int date)
{
int year = date / 10000;
int month = ((date - (10000 * year)) / 100);
int day = (date - (10000 * year) - (100 * month));
this.DateTimeObject = new DateTime(year, month, day);
}
You could URL encode it, but passing a DateTime around as a string is always a bit tricky because you may run into parsing errors if the request ever crosses culture boundaries.
A better option is to convert the DateTime to Ticks and pass that number around.
MVC uses the current culture when generating URL and binding Models. It makes sense in some scenarios (like when you have a textbox and the user enters the date there) but there are often problems. If you have different cultures then it is easier if values are always in the format for invariant culture.
In your case I would pass the value as a string rendered by invariant culture. Then I would use a CustomModelBinder to fill the property in the Model.

Resources