I have a layer with datetime field with milliseconds. I use the following script to get a QDateTime
object and add it to a memory layer:
layer = QgsVectorLayer("Point?crs=EPSG:4326", "test", "memory")
layer.dataProvider().addAttributes([QgsField("dt", QVariant.DateTime)])
layer.updateFields()
QgsProject.instance().addMapLayer(layer, True)
s = "22/04/20 05:14:38:560"
dt = QDateTime.fromString(s[:-3], "yy/MM/dd HH:mm:ss:zzz").addYears(100)
feat = QgsFeature(layer.fields())
feat.setGeometry(QgsGeometry()) # empty geometry for test
feat["dt"] = dt
layer.dataProvider().addFeatures([feat])
layer.updateExtents()
As in the image, although the millisecond part is missing in the attribute table, I can get whole datetime value including milliseconds as follows:
feat = list(iface.activeLayer().getFeatures())[0]
print(feat["dt"])
# vvv millisecond
# OUT: PyQt5.QtCore.QDateTime(2022, 4, 20, 5, 14, 38, 560)
But when I export the layer as CSV or Excel file using "Export > Save Features As...", it doesn't add the milliseconds part to the file:
dt,
2022/04/20 05:14:38
How can I export the layer to include the milliseconds part?