Convert datestring to date() in qml - string

i need to convert date string "28/01/2018" (dd/mm/yyyy) into a Date() in qml.
i'm tried this:
var dateBoard = masterPAGEMAIN.getData();
var locale = Qt.locale()
var someDateTest = new Date()
someDateTest = Date.fromLocaleString(locale, dateBoard, "dd/MM/yyyy");
var test = someDateTest.getDate().toString();
Also saw this:
conversione from string
, but my problem is that i continue to receive a "NaN" or "Invalid Date", how to can i get Date() from string in qml ?
Thanks

The string passed to fromLocaleString must be in the expected format. Try this code:
var dateBoard = "01/31/2018"
var someDateTest = Date.fromLocaleString(Qt.locale(), dateBoard, "dd/MM/yyyy")
var test = someDateTest.getDate() //nan
Since the string in dateBoard represents a date in MM/dd/yyyy format, fromLocaleString will return an invalid date and getDate nan, accordingly.
Same applies if dateBoard is an empty string, null or undefined.

Related

How to check if something is being returned as a string or a date as string?

In my app the api can return strings or dates as strings. I need to check if the api returns a date first. if it is not a date, just populate the field with the string. If it is a date, do the conversion before populating the field. The conversion and the populate the field part I have figured out, but how can I tell if it's returning a date as a string or a string? This is what the api returns:
data?.data?.nextAvailability
So I need it be something like
if data?.data?.nextAvailability is a string {
// do something
} else if data?.data?.nextAvailability is a date as a string {
// do this
}
You could maybe do something like this:
let string = data?.data?.nextAvailability
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
guard let date = dateFormatter.date(from: string) else {
print("not a date") // And do what you need to do
return
}
// is a date... do what you need to do with a date.
print(date)

How to remove time from date and time

In DOB i am getting date and time but i need only date
foreach (DataRow row in ProfileDt.Rows)
{
UserProfileData.FirstName = row["FirstName"].ToString();
UserProfileData.LastName = row["LastName"].ToString();
UserProfileData.Email = row["Email"].ToString();
UserProfileData.Address = row["HouseNo"].ToString();
UserProfileData.City = row["CityName"].ToString();
UserProfileData.date = row["DOB"].ToString();
}
If your database is giving you a string then you'll need to parse it to a date then back to a string with the desired date format.
string dateFormat = "MM/DD/YYYY HH:mm:ss" // REPLACE WITH THE CORRECT FORMAT
UserProfileData.date = DateTime.ParseExact(row["DOB"].ToString(), dateFormat ,
System.Globalization.CultureInfo.InvariantCulture).ToString("MM/DD/YYYY");
If your database is giving you a datetime directly (which is the better solution) then it becomes even easier.
UserProfileData.date = ((DateTime)row["DOB"]).ToString("MM/DD/YYYY");
Convert your string to DateTime type and use Date property, something like yourDate.Date
You can use DateTime.Parse or DateTime.ParseExact for parsing string to DateTime
It also make sense to return DateTime type directly from database so you do not need any conversions...

Node.js date format conversion without using modules

Date format conversion without using modules
The following code converts the date into different format: "2016-09-16T04:48:29.250Z"
var date = new Date().toISOString();
console.log(date);
How do I subtract 14 days and then convert the date into the same format?
var date = new Date();
date.setDate(date.getDate()-14);
console.log(date);
If it's already a regular Date object then seems like you pretty much had it already:
var date = new Date();
date.setDate(date.getDate()-14);
console.log(date.toISOString())
You're very close. Just do:
var date = new Date();
date.setDate(date.getDate() - 14);
console.log(date.toISOString());
Modify your code
var date = new Date();
date.setDate(date.getDate()-14);
console.log(date.toISOString())

NSDateFormatter.dateFromString returns nil ("yyyy-MM-dd" to NSDate)

Having a problem with NSDateFormatter.
So I have a date string formatted "yyyy-MM-dd", for example 2015-09-22.
However when I pass this into NSDateFormatter.dateFromString, the method returns nil. My code is as follows:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
let dateAsNSDate = dateFormatter.dateFromString(payload.objectForKey("messageExpiry") as! String)
From the debugger console at a breakpoint just above this code:
po payload.objectForKey("messageExpiry") as! String
"2015-09-31"
I have explored other questions similar to this but am as of yet still unable to fix this problem.
Any and all help would be appreciated.
Your code is correct and produces the expected result e.g. for
the input string "2015-09-22". For the input "2015-09-31" however,
nil is returned because that is an invalid date: September has
only 30 days.
You can set
dateFormatter.lenient = true
and then "2015-09-31" is accepted and treated as "2015-10-01".
But you should check first why payload.objectForKey("messageExpiry")
returns an invalid date.
var str = "2015-09-29"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
let dateAsNSDate = dateFormatter.dateFromString(str)
print(dateAsNSDate)
// -> "Optional(2015-09-29 07:00:00 +0000)\n"
So that would appear to work. Looks like you just got too many days in your month, so it wasn't a valid date! :)

Converting String to DateTime in SSJS

I'm trying to set a default value on an inputText field that has a dateTimeHelper associated with it. The following computed default value works:
return #Today();
However, I need to set the default date value 7 days from a date that is stored as a string on a document. So I tried the following:
var date:NotesDateTime = session.createDateTime(doc.getItemValue("EffDate")[0]);
log.logEvent("date= " + date); // returns 09/01/2013
var newDate = new Date(date);
log.logEvent("newDate= " + newDate); // returns NaN
var datePlus7 = #Adjust(newDate,0,0,7,0,0,0,"[InLocalTime]");
log.logEvent("datePlus7= " + datePlus7); // returns NaN
return datePlus7;
Which does not work because datePlus7 is NaN. Any ideas are greatly appreciated.
Use method toJavaDate() of DateTime class instead of Date constructor "new Date(date)":
var newDate = date.toJavaDate()

Resources