ASP.Net VB string to date based on format used in string - asp.net

I'm using a JavaScript datepicker that gives me the selected date based on the language. So when the language is Dutch I get an output like 21-09-2017 (dd-mm-yyyy) And for English 21/09/2017.
When I want to cast these Strings to Dates (CDate) I get a problem with the format. Day = Month or Month = Day. What is the best way to make a Date from a string based on the format used in the string?
A solution would be to write a function for each specific culture to handle the dates but i'm guessing there is a default function in .Net??

You can use DateTime.ParseExact to get what you want as shown here.
You can provide the format like so:
dateString = "15/06/2008 08:30" //Your Date time
format = "g" //General Fromat
provider = New CultureInfo("fr-FR") //French
result = Date.ParseExact(dateString, format, provider) //Parsed Result
this will result in: 6/15/2008 8:30:00 AM
This or course only works if you know the culture. Also you may want to check out the Date Time Format Strings found here.

Convert.ToDateTime(String).ToString("dd-MM-yyyy")
OR
DateTime.ParseExact(String, "dd/MM/yyyy", CultureInfo.InvariantCulture)

Related

Format Date via Parameter

I have a DateTime variable (default formatting), and I would like to format it to a format to I receive from a string parameter.
I normally do something similar to: {myDate:yyyy-MM-dd}, and it works properly.
Now I have a lot of possible date formats and need to format according to the chosen one.
I have tried the following but returned garbage (ae0aor0aa):
string testFormat = "yyyy. MM. dd.";
{myDate:testFormat }
I have also tried to convert the date to string and back to date with ParseExact, but gave me an invalid date exception. NB: the date in myDate is valid, as I have checked it with the debugger.
Can you kindly advise?
Thanks to apc, it was easily solved by myDate.ToString(testFormat)

How to convert the UTC date into local date in Moment.js?

I have a date in this format "2020-12-16T02:48:00" that came from the server. How can I convert this into local date and time? I tried some code but couldn't succeed.
Below is the attempt that I had made in angular after receiving date from the server.
response.data.map(date=>{
var centralDate = moment( date).zone("-06:00");
date = moment(centralDate).local().format('YYYY-MM-DD hh:mm:ss');
})
If indeed the value is in UTC (as per the title of your question), and it looks like "2020-12-16T02:48:00", and you want to convert it to local time, then you should do the following:
moment.utc(date).local().format('YYYY-MM-DD HH:mm:ss');
That does the following:
Parses the input in terms of UTC
Converts it to local time
Formats it as a string in the given format
Note also that you had hh in your original format. That is for hours in a 12-hour time format and thus you shouldn't use it without also using either A or a to indicate AM/PM or am/pm. Otherwise HH is for hours in a 24-hour time format.
If your issue is that the timezone doesn't change you can resolve using utcOffset (https://momentjscom.readthedocs.io/en/latest/moment/03-manipulating/09-utc-offset/) in this way:
response.data.map(date=>{
date = moment( date).utcOffset(-360);
})
Where 360 is the conversion fo the hours in minutes
var d= new Date();
d = new Date(d+ "Z")
I am not an expert in angular but I guess the trouble in your date is the word “T”. May be using string removal function you can remove the word “T” and then it becomes a proper date time value?

How to change UK date format in LogicApp

Im trying to convert a U.K. input date (dd-MM-yyyy) to format (yyyy-MM-dd)
I tried
"#formatDateTime('15-03-2019','yyyy-MM-dd')" ==> Error
but got error:
'In function 'convertTimeZone', the value provided
for date time string '15-03-2019' was not valid. The datetime
string must match ISO 8601 format.'
How do I go about converting this input date? The input format is (dd-MM-yyyy) and cannot be changed.
I can easily convert from (MM-dd-yyyy) as shown below, but im not able to convert from (dd-MM-yyyy)
"#formatDateTime('03-15-2019','yyyy-MM-dd')" ==> OK
Date and time functions provided by azure logic app cannot recognize the timestamp in dd-MM-yyyy format.
After my research, there is no existing function that can directly solve this problem, but you can use substring and concat to deal with this problem.
The workflow of the logic app looks like this:
The expression of the formatDataTime:
formatDateTime(concat(substring(<your-date-string>,6,4),'-',substring(<your-date-string>,3,2),'-',substring(<your-date-string>,0,2)),'yyyy-MM-dd')

How to format the datetime in c#?

I got the current datetime by using the following code.
DateTime dt = DateTime.Now;
But it is in the format of, 2015-02-23 17:25:07.123
how to convert this to the format of, "02/23/2015"?
"But it is in the format of"
No it isn't. It's just a DateTime. If you want a particular text representation, call ToString on it, specifying the format. For example:
DateTime now = DateTime.Now;
string formatted = now.ToString("MM/dd/yyyy");
You might also want to specify the culture to use for formatting - that way you could just say "Use the right short date format" for example.
See the MSDN pages on custom date/time format strings and standard date/time format strings for more details.
Try this,
String.Format("{0:MM/dd/yyyy}", dt);

How to convert date to mm/dd/yyyy format

I want to convert dateformat to mm/dd/yyyy. Whatever dateformat is coming in textbox, I want to convert it into mm/dd/yyyy.
First you need to get it into a datetime object. The most common standards work via:
DateTime x = DateTime.Parse(txtDate.Text);
If you expect a freaky format, you still have to know what format it is:
DateTime x;
DateTime.TryParseExact(txtDate.Text, "YYddd", out x);
Then simply output the data:
string date = x.ToString("MM/dd/yyyy");
But you really need to enforce your formatting using regex, validators, scout's honor - something.
see MSDN for full details.
You will need to parse the input to a DateTime object and then convert it to any text format you want.
If you are unsure what format you may be getting, maybe it is a good idea to restrict the user to a single format (using validation or better yet a date picker).

Resources