Flutter Cloud Firestore convert serverTimestamp to String - firebase

My flutter app needs to get the current Time, I already tried several packages for this like: truetime, ntptime, Datetime from dart, Timestamp.now from cloud firestore they were all returning different times when switching the timezone in the phone settings. The only method were it was returning the same time for all devices on all time zones was the FieldValue.serverTimestamp() method, Im saving this value into a firestore document and there it recognizes it as a Field of Type Timestamp.
I want to convert this into a String so that I get a String like this: "year-month-day hours:minutes:seconds"
what I tried:
(1)
final now = FieldValue.serverTimestamp();
saving this now variable into a document ... , document field looks like this:
timestamp: Day.Month Year at Hours:Minutes:Seconds UTC+2 (Timestamp)
then Im taking this value from the field "timestamp" and assign it to a text widget:
Text(doc.data["timestamp"].toString())
Text Widget looks like this:
Timestamp(seconds=1568560057, nanoseconds=790000000)
what it should like(as already said):
"year-month-day hours:minutes:seconds"
(2)
final now = FieldValue.serverTimestamp().toString();
saving this now variable into a document ... , document field looks like this:
timestamp: "Instance of 'FieldValue'" (String)
then Im taking this value from the field "timestamp" and assign it to a text widget:
Text(doc.data["timestamp"].toString())
Text Widget looks like this:
Instance of 'FieldValue'
what it should like(as already said):
"year-month-day hours:minutes:seconds"
So anyone knows how I can get the String format I need or maybe another method which is returning same time for all devices and time zones and which is convertable into a String of the format I need?
Thanks in advance!
SOLUTION:
Thanks to Frank and CopsOnRoad
We have to save this value into a document field:
final now = FieldValue.serverTimestamp();
this will be saved as a Timestamp object in the document field.
now we need to convert it to a DateTime Object for using it in a Text Widget for example and then convert this to a String to get the format:
Text(doc.data["timestamp"].toDate().toString())
Text Widget looks like this:
Year-Month-Day Hours:Minutes:Seconds
solved.

When you read a timestamp from a document in Cloud Firestore you get back a Timestamp object.
To convert this to a regular date object, you can call the toDate() method on it.
And then you can format that Date object in any way you'd usually do, e.g. with the DateFormat class as shown in Date Time format in Flutter dd/MM/YYYY hh:mm

Related

Can't Format Date in GraphQL in Gatsby Site

I am having trouble formatting a date in Gatsby. I have sourced some data from firestore. One of the fields that I have sourced is called datePublished and it contains a number in the form of a timestamp (e.g., 1576945502000). Indeed, it really is a timestamp, but it got saved to firestore in the number format type.
Now, I would like to format the datePublished field using the formatString function in graphql -- as noted here: https://www.gatsbyjs.org/docs/graphql-reference/#dates
But I can't get the formatting to work. Whether I try to format the string in graphiql or on my site, it does not work. Here is the code that I tried:
query MyQuery {
allNewsFeed {
edges {
node {
published(formatString:"dddd Mo, YYYY")
}
}
}
}
And this is the error message that I get in graphiql:
Unknown argument "formatString on field 'datePublished' of type 'NewsFeed'.
If I dig a little deeper, I notice that the field type in firestore is listed as number and in the graphiql document explorer it is listed as a FloatQueryOperatorInput.
What's more, I did a little experiment where I tried to save the same number as a string, number and timestamp in firestore. Neither the string or number format worked and for whatever reason, the gatsby-firesource plugin will not pull in the field with a timestamp format.
So I am wondering, what do I have to do to get this to work in gatsby?
Any ideas?
Thanks.
I had a similar situation with Gatsby not accepting "formatString" on a date field, for me what solved the issue was to change the format of the field to YYYY-MM-DD so it went from 02 06 2017 to 2017-06-02 and gatsby got the hint that that field represents a date.
It seems gatsby "reads" the data and guesses the type and based on that we get certain functionality.
Since gatsby uses moment.js I'm guessing the YYYY-MM-DD format is picked up by it.
I'm using MDX so it was really easy for me to change the format of my date field since it's just plain text, I'm not sure you have the same luxury with firestore but I hope this at least give you some ideas to try out. Good luck!

How to convert string field into integer field in kibana

So i am sending data of event viewer through winlogbeat to kibana. Actually my event_data.boottime value is coming in string format and i wanted this data field in number format. I have tried changing the format in winlogbeat.template.json but its not working.boot time field
You need to make sure the mapping is defined correctly so that the data is automatically converted and save in required format by elastic.
"boot_time": {
"type": "long"
}
You can check the current mapping of your type by using
http://elasticip:port/indexname/typename/_mapping

icCube dateTime format and Excel PivotTable TimeLine

I have a flat file datasource that I'm using to construct a Dimension. One of the columns is a DateTime in the format: yyyy.MM.dd HH:MM, which I specified as the 'Date Converter Pattern'.
However, when I connect to my cube using an Excel Pivot Table, the date format defaults to: yyyy-MM-DDTHH:MM:ss.SSS and when I try to create a Timeline on my PivotTable, excel does not recognize this as a valid DateTime format.
is there any way to change this in the config files
thanks
To create a TimeLine Excel is using several DISCOVER requests and is more or less assuming it is talking to SSAS to discover an " attribute " hierarchy. Support has been added into icCube (from 6.0.1) to flag a hierarchy as an Excel TimeLine (new field in the hierarchy definition): this hierarchy must have an ALL level and a single DATE (i.e., days) level below.
I'm a bit lost, I understand you've converted to a DateTime the strings from the data source. Why do you see 'yyyy-MM-DDTHH:MM:ss.SSS' as the member names/labels ?
You can choose whatever you want to ?
My educated guess, Excel is taking for the timeline the key of the member not the label (that needs to be a DateTime object in the datasource).
Can you clarify ?

Date format in lotus notes

I have a lotus notes field which should save the date/time in the GMT format,
for that I used
Dim timenow As Variant
timenow = Now()
Dim dateTime As New NotesDateTime( timenow )
doc.abc = dateTime.GMTTime
This will set the field 'abc' to have the date and time in GMT. But now I am having issues with the date format. In my system it saves it in the format 10/28/2016, but for other users whose system date format is different, it saves it in the format 28.10.2016. I need to force the date format to be 10/28/2016, I tried used format function
doc.abc = Format(dateTime.GMTTime, "m/d/yy h:nn")
The above code gives the date and time in GMT, but doesn't change the date format.
You are wrong in the assumption, that the date is SAVED in that format.
Date items in the backend are number- items. They store the date as number, the integer part is the day, the fraction part is the time of the day (day 0 is 12/31/1899 00:00)
Then the setting in the client determins, how the client displays the date.
In the properties of the item you usually define "Client" as display format, but you could fix the display of the date to a specific form.
But usually this is NOT necessary, and every german will not like the "reversed" order of english / american time formatting.
This will only be a problem, if you construct a text from that date, as #Text() will convert it using the clients format.
I guess, that your problem is not in the "saving" of the item, but somewhere else in your code, where you interpret the date as text, and this is always a problem.
What type of field is it? If it's a date field, the Notes client will use the user's local date format.
If you want to use a specific format, you can use a text field instead, but of course then the time won't adjust to the user's local time zone.
The way to get the best of both worlds is to store the date in date field, but use a computed-for-display field to show it in the user's current timezone, but in exactly the format that you want.
Most people use a NotesDateTime object to set the date in a field
Dim ExpiryDate As New NotesDateTime(Cstr(Today))
Even if the field in the form uses a specific format, the date like 2019-09-08 can mean 8th september 2019 or 9th august 2019 depending of the LocalDate setting
To avoid this behavior, you need to force the format in your NotesDateTime Object
Like this
Dim ExpiryDate As New NotesDateTime(Format$( Today, "yyyy-mm-dd "))

How To store Value form QDateEdit in String?

I want to store value from QDateEdit to String. I'have used following code:
QString str = ui->dateeditobject->date().toString("dd/mm/yyyy")
But the problem is, that the month value is not being stored as you can see in the picture.
Where am I going wrong?
Month is MM (uppercase), otherwise it is minutes
ui->dateeditobject->date().toString("dd/MM/yyyy")
Documentation here.

Resources