How to format Date from QString in QML - qt

I am using QML Calendar and would like to set property of maximumDate and minimumDate.
According to the documentation
By default, this property is set to the latest maximum date (25 October, 275759 AD)
so I made an API that returns me this date in string format "25 October, 2018 AD"
when i try set
maximumDate = "25 October, 2018 AD"
QML throws me an error that i cannot assign QString to QDateTime
My question is how do format this string to date inorder for maximumDate to accept it. I was hoping to do it only in QML

There is no implicit conversion between a string and QDate, there is however one that works bidirectionally for QDate and qml's Date.
However, it doesn't seem like dates work with the format you are trying to use. I think you are confusing the stated date value as a literal value, when in fact it is just a human readable date.
So instead, you should try this:
maximumDate = Date.fromLocaleString(Qt.locale(), "2018-10-25", "yyyy-mm-dd")

Related

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]

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

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)

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.

DateTime format conversion to string ignores AM/PM

I am trying to convert DateTime object to string using formatting but due to some strange reason, it ignores AM/PM. It would just take the magnitude of the Hour in the object.
I am using the following format:
StartDate.ToString("yyyy-MM-dd hh:mm:ss.fff");
and
String.Format("0:yyyy-MM-dd hh:mm:ss.fff", StartDate);
I don't think that there is a difference between the two, but just wanted to give it a try. If I pass a value 4/28/2012 6:00:00 AM or 4/28/2012 6:00:00 PM, the result is the same "2012-04-27 06:00:00.000"
You've used hh which uses the 12-hour clock. You want HH which uses the 24-hour clock.
See MSDN for more details about custom format strings.
Note that you may wish to specify the invariant culture, unless you really want the time separator to depend on the current culture:
string formatted = StartDate.ToString("yyyy-MM-dd HH:mm:ss.fff",
CultureInfo.InvariantCulture);
(Note that you shouldn't have the braces if you're passing the format string to ToString. I'll assume this was just a typo in the question.)
If you want to use the 12-hour clock, use tt in the format string to produce the AM/PM designator.

Reporting Services Expression for Week of Year

I have a Report that I send a parameter to as 'WeekStart'. This is based on a selection a user makes on a datepicker.
I'm using the following to extract the week of the year:
=DatePart("ww", Parameters!WeekStart.Value)
The problem I'm having is that when I pick the day 03/01/2012 (dd/MM/yyyy format), the week of the year is returned as 9, which would technically be true had the date been 03/01/2012 with a dateformat of MM/dd/yyyy.
I've tried using CDate, FormatDateString etc but nothing seems to be working. I either get #Error or it returns as the 9th week of the year.
Any suggestions?
What you can do is this:
use YourDatabase
go
set dateformat dmy
go
That will set the date format for your database, and that should get your DATEPART function working as expected.
SET DATEFORMAT (MSDN)

Resources