I have a global 'ru' setting for moment, but i need to parce 'en' date in one of the functions. How can i do it?
Sandbox.
import React from "react";
import moment from "moment";
import "moment/locale/ru";
// global
moment.locale("ru");
const parceEngDate = () => {
return moment("Nov 04, 2019", "MMM D YYYY").locale("en").toDate().toString();
};
export default function App() {
// Fri Jan 04 2019 00:00:00 GMT+0300
// It's november, not january!
return <div className="App">{parceEngDate()}</div>;
}
You can use moment(String, String, String), so in your case moment("Nov 04, 2019", "MMM D YYYY", "en").
Snippet:
console.log( moment("Nov 04, 2019", "MMM D YYYY", "en").toDate().toString() );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js"></script>
Related
i have created a struct and it contains two time.Time formatted fields, named with json tags: start_time and end_time.
type MyStruct struct {
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
}
when i'm trying to send a PUT request over HTTP using gin framework to update those values, the time format which i'm sending, changes in sent struct.
what i'm sending:
curl -X PUT -H 'Content-Type: application/json'
http://my_address -d '{
"start_time": "2021-04-27T22:24:31Z",
"end_time": "2021-11-01T22:24:31Z"
}'
what it receives:
start_time="2021-04-27 22:24:31 +0000 UTC",
end_time="2021-11-01 22:24:31 +0000 UTC",
in the other hand,
i'm saving the struct in a couchbase and as returning value of query, i'm sending back the document(my struct):
my query:
Update BucketName as e
set start_time="2021-04-27 22:24:31 +0000 UTC",
end_time="2021-11-01 22:24:31 +0000 UTC" where ( my document equality condition)
returning e
and it executes with no errors.
when i'm trying to read the returned struct,
my code to reading it:
var s domain.MyStructSample //
err = result.One(&s)
if err != nil {
if err == gocb.ErrNoResult {
return nil, errors.New("there is no result")
}
logger.ZSLogger.Errorf("error on update one item from my struct with error :%s", err)
return nil, err
}
gocb generates errors on those time items and here is the error:
"message":"error on update one item from my struct with error :parsing time \"\"2021-11-01 22:24:31 +0000 UTC\"\" as \"\"2006-01-02T15:04:05Z07:00\"\": cannot parse \" 22:24:31 +0000 UTC\"\" as \"T\""}
by the way, as i said, update is done with no errors ( query executes with no errors).
so what should i do with it ?
How did you generate this query:
Update BucketName as e
set start_time="2021-04-27 22:24:31 +0000 UTC",
end_time="2021-11-01 22:24:31 +0000 UTC" where ( my document equality condition)
returning e
As the error says, time data stored in couchbase should be in format RFC3339 (2006-01-02T15:04:05Z07:00) instead of the default 2006-01-02 15:04:05 -0700 MST, so maybe you should insert data with query:
Update BucketName as e
set start_time="2021-04-27T22:24:31Z07:00",
end_time="2021-11-01T22:24:31Z07:00" where ( my document equality condition)
returning e
If you have problem formatting time, read the doc https://golang.cafe/blog/golang-time-format-example.html
And, as #MrFuppes commented, if you need to customize JSON output format, read this How to format timestamp in outgoing JSON
Ok so I have this moment object I'm looking at in Developer Tools:
amoment: Moment
_a: (7) [2017, 4, 21, 20, 15, 0, 0]
_d: Sun May 21 2017 15:15:00 GMT-0500 (Central Daylight Time) {}
_f: "MM/DD/YYYY h:mm A"
_i: "5/21/2017 8:15:00 PM"
_isAMomentObject: true
_isUTC: true
_isValid: true
_locale: Locale {_calendar: {…}, _longDateFormat: {…}, _invalidDate: "Invalid date", ordinal: ƒ, _dayOfMonthOrdinalParse: /\d{1,2}(th|st|nd|rd)/, …}
_offset: -180
_pf: {empty: false, unusedTokens: Array(0), unusedInput: Array(1), overflow: -1, charsLeftOver: 3, …}
_strict: false
_z: Zone {name: "America/Halifax", abbrs: Array(229), untils: Array(229), offsets: Array(229), population: 390000}
__proto__: Object
As you can see, the text representation is 5/21/2017 8:15:00 pm. This is the correct time in UTC and I notice "_isUTC" is "true".
The _d property shows that datetime in CDT, where I am now.
The _z property shows a Zone object set to "America/Halifax"--this is dynamically set elsewhere, but also correct.
I would like to return a moment reading "5/21/2017 5:15:00 pm" because that's what that datetime is in "America/Halifax."
There are so many moment.js functions I haven't found the right way to do this.
All of the fields that begin with underscores (_) are meant to be internal to Moment. You should not be using them at all. More on this here, and here.
To output a string in a particular format from a moment object, use the format function. For example:
yourMomentObject.format("M/D/YYYY h:mm:ss a") //=> "5/21/2017 5:17:00 pm"
I ended up doing new Date(amoment.format('YYYY-MM-DDTHH:mm:ss+00:00')); which worked for my purposes. The zone kept wanting to render the date in that zone, so I just had to omit it.
I have a string as 2018-10-15 11:40:39 and I have to convert this into a string like 10-15-2018 11:40 AM. i'm beginner in react native.
From the root of your project just run:
npm install moment --save
Then you can import it in your code:
import moment from 'moment';
For date convert, use below code
console.log( moment('2018-01-01 13:40:39').format('MM-DD-YYYY hh:mm a') );
alert( moment('2018-01-01 13:40:39').format('MM-DD-YYYY hh:mm a') );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
I'm trying to parse email date field to Logstash #timestamp field:
Example email date filed: Mon, 28 May 2018 15:53:54 +0100
Logstash config: filter {date {match => ["date", "E, d MMMM yyyy HH:mm:ss Z"]}
Result #timestamp: May 28th 2018, 14:53:54 +0000
As you can see Logstash haven't added +0100.
Could you please tell me where I have done wrong?
Using Qt 5.6.0 & MSVC2015 I have an app which upon start-up, using QProcess and ssh/plink/cat, shall attempt to read the hostname of a remote server and capture the contents of a specific file on a remote server. This works when run from Qt Creator (the IDE), in either debug or release. If I attempt the same app from outside if Qt Creator, either the signal is never emitted or the slot is not called.
Here's the pertinent code bits:
void MainWindow::Perform1stSSHcmd()
{
QPalette palette;
// hostname: THIS IS OUR 1st SSH COMMAND SO WE WANT TO DETERMINE IF THE KEYS ARE SET UP OK...
QProcess* qp = new QProcess( this );
QString userAndCommand = "root#";
userAndCommand.append( m_cmsIp );
userAndCommand.append(" hostname"); // cmd we want to execute on the remote server
qp->start( m_plinkPuttyCmd.arg( userAndCommand ));
qp->waitForFinished( 16000 ); // I've tried vaious values for this
QString output = qp->readAll();
QString err = qp->readAllStandardError();
// ... SNIP various error checking here ...
// Now the system info
m_sysInfoProc = new QProcess(this);
qDebug() << "About to read systemInfo.xml... ";
if( !connect( this->m_sysInfoProc, SIGNAL( readyReadStandardOutput() ), SLOT( readSystemInfoXML() ))) {
qDebug() << "Connect Failed!!!!!";
}
userAndCommand = "root#";
userAndCommand.append( m_cmsIp );
qDebug() << "preparing cat of xml... ";
userAndCommand.append(" cat /root/systemInfo.xml");
m_sysInfoProc->start( m_plinkPuttyCmd.arg( userAndCommand ));
qDebug() << "->start issued... ";
m_sysInfoProc->waitForFinished( 6000 );
qDebug() << "after waitForFinished( 6000 )";
}
void MainWindow::readSystemInfoXML()
{
qDebug() << "In readSystemInfoXML()";
QProcess *systemInfoXml = qobject_cast<QProcess *>(sender());
if( !systemInfoXml ) {
return;
}
QString res = systemInfoXml->readAllStandardOutput();
qDebug() << "readSystemInfoXML() just read:" << res;
if( !res.length() ) {
return;
}
// . . . XML parsing of file contents . . . (not a concern, works fine)
}
Output Debug/Releas Mode from IDE:
Wed Sep 28 15:36:06 2016 Debug: output: "Lanner"
Wed Sep 28 15:36:06 2016 Debug: err: ""
Wed Sep 28 15:36:06 2016 Debug: About to read systemInfo.xml...
Wed Sep 28 15:36:06 2016 Debug: preparing cat of xml...
Wed Sep 28 15:36:06 2016 Debug: ->start issued...
Wed Sep 28 15:36:06 2016 Debug: In readSystemInfoXML()
Wed Sep 28 15:36:06 2016 Debug: readSystemInfoXML() just read: "\n \n\t2.50-06-15\n 1.0\n \tSINA\n \tclass IC\n \tdoes something\n \n"
Wed Sep 28 15:36:06 2016 Debug: after waitForFinished( 6000 )
Wed Sep 28 15:36:06 2016 Debug: ICICIC
(tags missing from xml dur to formatting, only values appear)
Now the release...
Output when started from release folder on my Windows box:
Wed Sep 28 15:38:09 2016 Debug: output: ""
Wed Sep 28 15:38:09 2016 Debug: err: ""
Wed Sep 28 15:38:09 2016 Debug: About to read systemInfo.xml...
Wed Sep 28 15:38:09 2016 Debug: preparing cat of xml...
Wed Sep 28 15:38:09 2016 Debug: ->start issued...
Wed Sep 28 15:38:09 2016 Debug: after waitForFinished( 6000 )
Wed Sep 28 15:38:09 2016 Debug: ICICIC
I've searched for others who may of had similar issues, the closest was
Qt: some slots don't get executed in release mode
I've tried touching MainWindow.h, re-running qmake and build all, no luck.
Any advice would be greatly appreciated.
Ian
SOLUTION: As it turns out it was related to the way ssh caches keys, more specifically my app's processing of QProcess' output.