How to convert string “M/dd/yyyy hh:mm:ss tt” to "hh:mm tt" DateTime format from English to Spanish in Xamarin forms? [duplicate] - datetime

Probably a simple question -
I'm reading in data from a number of files.
My problem is, that when I'm reading in the date from an american file, I parse it like so:
DateSold = DateTime.Parse(t.Date)
This parses the string t.Date into a date format, however it formats the american date to a european date, e.g.
If the date is in the file as 03/01/2011, it is read as the 3rd of January, 2011, when it should be the 1st of March 2011.
Is there a way of doing this so that it formats to the european date?

var dt = DateTime.ParseExact(t.Date, "MM/dd/yyyy", CultureInfo.InvariantCulture);
The DateTime itself has no formatting, it is only when you convert it to or from a string that the format is relevant.
To view your date with American format, you pass the format to the ToString method
string americanFormat = dt.ToString("MM/dd/yyyy");

If you are parsing the date from a file which is specifically a US formatted file then simply pass the US culture information into the parse function as follows;
var usCulture = "en-US";
var dateValue = DateTime.Parse(dateString, new CultureInfo(usCulture, false));
This way you can simply swap out the culture string per different region required for parsing. Also, you no longer have to research the specific datetime format nuances for each culture as .Net will take care of this for you as designed.

Use DateTime.ParseExact or DateTime.TryParseExact when parsing, and specify a format string when you format with ToString too.
Note that there's no such thing as "an American date" after it's been parsed. The DateTime value has no concept of formatting.
It sounds like you're not actually interested in the Parse part so much as the formatting part, e.g.
string formatted = dt.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
... but I would recommend that you control both the parsing and formatting explicitly.
If you have different file formats, you'll need to give different format strings when you read each file. How you then format the data is a separate decision.

If you know the format ahead of time, you can use DateTime.ParseExact, using the American format as your format string.

string formatteddate=DateTime.Now.ToString("d") // output: 11/8/2012
string formatteddate=DateTime.Now.ToString("D") // output: Monday, November 08, 2012
string formatteddate=DateTime.Now.ToString("f") // output: Monday, November 08, 2012 3:39 PM
string formatteddate=DateTime.Now.ToString("g") // output: Monday, November 08, 2012 3:39:46 PM
string formatteddate=DateTime.Now.ToString("d") // output: 11/8/2012 3:39 PM
More date-time format in asp.net is given here.
http://dateformat.blogspot.in/2012/09/date-time-format-in-c-aspnet.html

Related

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

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)

How to convert a String into an Ecto.DateTime in Elixir?

I need to convert a string containing a valid UTC time to an Ecto.DateTime one, which I will insert it into my database with the correct format later. I have tried using the Ecto.DateTime.cast(date) method but it doesn't seem to work. The string is Sat Aug 04 11:48:27 +0000 2012 and comes from the Twitter API.
I know there are libraries such as Timex which I didn't inspect yet. Is there any easy working solution already built in Elixir?
There's no built-in solution in Elixir or Erlang for parsing DateTime values of this format:
Sat Aug 04 11:48:27 +0000 2012
You can certainly write a parser yourself, but it's neither going to be short or simple. You'll have to split the string, get the values of both date and time parameters, convert month strings to month integers, parse the timezone, represent the complete value in Elixir/Erlang DateTime formats and then finally cast it to Ecto.DateTime. See the following links:
Elixir Tips - Date Parsing
Erlang - How Can I Parse RFC1123 Dates Into An Erlang Term?
Convert timestamp to datetime in erlang
Using Timex is the best option here.
It's a well written library that allows you to stay away from the chaos of inner workings of Date/Time. With Timex, you can parse your string like this:
"Sat Aug 04 11:48:27 +0000 2012"
|> Timex.parse!("%a %b %d %T %z %Y", :strftime)
|> Ecto.DateTime.cast!
# => #Ecto.DateTime<2012-08-04 11:48:27>
Note: Timex has built-in support for a lot of the common DateTime formats, and I found it weird that a DateTime format being sent by Twitter wasn't supported - so I wrote one for you. Maybe double check to see if your string is correct? Also take a look at Timex Parsing and Formatting documentation.

QDateTime Conversion

I need to convert the String variable to QDateTime format
my code looks
QString date ="Thu Jun 18 2015";
QDateTime tmp = QDateTime::fromString(date,"ddd MMM dd yyyy HH:mm:ss");
But the result is Thu Jan 1 00:00:00 1970.
Later I have to convert this date in to foramt yyyy-MM-dd HH:mm:ss, so as a first step I have convert the string in to QDateTime then I have to convert to the final format, is there anything mistake with the above code?
Any help will be appreciated.
Thanks
Haris
Your date string does not include a time, while you mentioned that you want one, this will fail at least in Qt 5.4 .
I don't know though why you get the epoche outputed, maybe that is dependant on your Qt version.
Your date format is also locale dependent.
See for example the doucmentation for "ddd" in QDateTime::fromString:
the abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses QDate::shortDayName().
Which unfortunately is not that clear, while it is more clear for QDateTime::toString:
the abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses the system locale to localize the name, i.e. QLocale::system().
For example, in my locale (German, Austria) "ddd" for Thursday results in "Do." which is different from "Thu" and makes it impossible to parse English abbrevations with that locale.
To ensure you are using the correct locale when reading or writing locale dependent output use QLocale.
In your case that would be QLocale::toDateTime:
QLocale locale(QLocale::English, QLocale::UnitedStates);
QDateTime dt = locale.toDateTime("Jun 18 2015", "MMM dd yyyy");
Then if you also want locale dependent output use QLocale::toString.

ColdFusion - DateTime Format with GMT offset

I am using ColdFusion 10 to make some REST calls and the date returned is using a GMT offset.
Example: 2013-03-25T14:30:40-04:00
I need this formatted for 2 purposes:
Screen Display so it looks something like mm/dd/yyyy hh:mm:ss
To Insert into mySQL.
I have tried a variety of the CF time/date functions but continue to get the "is not a valid date format"
I thought maybe the #ParseDateTime(i.submitted_at,"pop")# would handle it with POP but same issue.
Spent a few hours now trying multiple variations and googling around now just going in circles. Any ideas would be greatly appreciated.
Thanks!
Have a look at the UDF DateConvertISO8601() on CFLib.
DateConvertISO8601(ISO8601dateString, targetZoneOffset) on CFLib
Description:
This function take a string that holds a date in ISO 8601 and converts it to ODBC datetime, but could be adapted to convert to whatever you like. It also will convert to a datetime in a timezone of your choice by specifying the offset, i.e. it could take a datetime in GMT and convert to PT. See http://www.w3.org/TR/NOTE-datetime for description of ISO 8601, the International Standard for the representation of dates and times.
Return Values:
Returns a datetime.
The source code is viewable at the link I provided.
This, 2013-03-25T14:30:40-04:00, is a string. If you run this:
x = left(2013-03-25T14:30:40-04:00, 19);
you get 2013-03-25T14:30:40. You can use the replace function to replace the T with a space. You can then to this
DateTimeVar =parsedatetime('2013-03-25 14:30:40');
Now you have a datetime variable that you can format. If necessary you can do a datediff with the offset from GMT.
This is an informational answer, not a direct answer to the question.
ColdFusion 11 has updated the ParseDateTime() function so that it will correctly convert the ISO-8601 date/time strings to a ColdFusion datetime object.
With a number of remote requests and responses, the date / time values can often be returned in ISO format. In your case, the mask looks like this:
YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
In this ISO format, the T string is a literal representation of a marker where the time stamp starts in the string (with the offset following directly).
Below is a reusable function that will convert an ISO date format into a useable ColdFusion date time object:
<cffunction name="ISOToDateTime" access="public" returntype="string" output="false"
hint="Converts an ISO 8601 date/time stamp with optional dashes to a ColdFusion
date/time stamp.">
<cfargument name="Date" type="string" required="true" hint="ISO 8601 date/time stamp." />
<cfreturn ARGUMENTS.Date.ReplaceFirst(
"^.*?(\d{4})-?(\d{2})-?(\d{2})T([\d:]+).*$",
"$1-$2-$3 $4"
) />
</cffunction>
You can then call the function like so to output or return a ColdFusion-friendly version of the date time:
ISOToDateTime( "2013-03-25T14:30:40-04:00" )
That function is courtesy of Ben Nadel. The original blog post can be found here:
http://www.bennadel.com/blog/811-Converting-ISO-Date-Time-To-ColdFusion-Date-Time.htm
You can also convert the date time value using the offset, if required. Again, Ben Nadel has a great blog post outlining how to accomplish this:
http://www.bennadel.com/blog/1595-Converting-To-GMT-And-From-GMT-In-ColdFusion-For-Use-With-HTTP-Time-Stamps.htm
CF10 can use this code as stated in the example of the parseDateTime() doc.
<cfset string = "1997-07-16T19:20:30+01:00">
<cfset date = parseDateTime(string, "yyyy-MM-dd'T'HH:mm:ssX")>

Javascript ASP.net date format without timezone info - timezone offsets

I have a client side JavaScript that generates a date in JavaScript( new Date(2007,5,1)).
I need this date passed through to a hidden field that the code behind can access.
My issue is that when the hidden field is converted into a DotNet datetime, the time is incorrect. This is because the JavaScript is including timezone info from the client browser.
DotNet is then using this info to recalculate the time based on the difference between the server time and the client time.
What i need from the JavaScript is just the year, month and day.
I don't want to pass through 3 int values to my code behind as this will be a major change to the whole app.
What is the best way for me to accomplish this?
If i can set a UTC time with no timezone info I think that might work.
Any help is appreciated.
demo
If I understood it correctly,
you need .toDateString()
var date = new Date(2007,5,1);
document.write(date);
document.write("<br><br>versus<br><br>");
document.write(date.toDateString());
prints
Fri Jun 01 2007 00:00:00 GMT+0800 (Taipei Standard Time)
versus
Fri Jun 01 2007
You can use DateTimeOffset.ParseExact to parse a string to a DateTimeOffset value using the format you specify:
string dateString = "Fri Jun 01 2007 00:00:00 GMT+08:00";
DateTimeOffset date = DateTimeOffset.ParseExact(dateString, "ddd MMM dd yyyy hh:mm:ss 'GMT'zzz", CultureInfo.InvariantCulture);
You have to put GMT in quotes otherwise M will be interpreted as a format character.
Unfortunatelly, it is not possible to ignore part of the string value. If your string includes the name of the timezone you have to split it first and get the part without the description
string dateString = "Fri Jun 01 2007 00:00:00 GMT+08:00 (Taipei Standard Time)";
var parts=dateString.Split('(');
string datePart = parts[0].TrimEnd();
var date=DateTimeOffset.ParseExact(datePart,"ddd MMM dd yyyy hh:mm:ss 'GMT'zzz",CultureInfo.InvariantCulture);
You can build up a string from the javascript Date object you have created - it has getDate(), getMonth() and getFullYear() methods that you can use to build up the exact string you want in the hidden field.
I would recommend to use a format specification in C# when you get the values in the code behind file. Let me explain what I mean -
The date time format for the Date(...) in JavaScript is as follows
"Tue Jun 1 11:12:15 UTC+0530 2010"
which in C# would translate to the following format string -
"ddd MMM d hh:mm:ss UTCzzz yyyy"
with this format string use the DateTime.ParseExact(string <Hidden Field Value>, format, provider) to get the correct value for the datetime in C#.
Use provider as System.Globalization.CultureInfo.InvariantCulture.

Resources