Mongo saving ISODate - mongodb

Im currently saving the next date into a collection
2022-01-02T00:00:00-05:00
But somehow when I check the date in the db looks like this (adding 16 seconds)
2022-01-02T05:00:16.000+00:00
When It should be saved like this
2022-01-02T05:00:00.000+00:00
Someone know why is this happening?
EDIT: I found out that after I convert excel to json the 16 seconds are being added, this is how I convert excel to json with the package "convert-excel-to-json
"
const convertExcel = async (path) => {
const result = excelToJson({
sourceFile: path,
sheets: [
{
name: "Sheet1",
header: {
rows: 1,
},
columnToKey: {
A: "DATE",
},
},
],
});
Collection.insertMany(result.Sheet1);
};
Im getting the date like this:
{
DATE: 2022-01-02T05:00:16.000Z,
}
Excel:

Related

replace the filename of transferred file from gmail to gdrive with filenameDATE

I have script that can transfer email from gmail to gdrive and my plan is replace the filename of the email attachment to assigned filename with current date ddmmyy (Ex. project012322.xlsx) or .csv when the file is transferred to gdrive.
May I know what I need to modify or add with my script?
const searchItem = "in:inbox subject:(My Project) has:attachment";
const threads = GmailApp.search(searchItem, 0, 100);
const ids = threads.flatMap((thread) => {
const messages = thread.getMessages();
return messages.map((message) => {
const id = message.getId();
if (!values.includes(id)) {
const attachments = message.getAttachments({ includeInlineImages: false, includeAttachments: true });
attachments.forEach((attachment) => {
Drive.Files.insert({ title: attachment.getName(), mimeType: attachment.getContentType(), parents: [{ id: folderId }] }, attachment.copyBlob());
});
}
return [id];
});
});
Your script contains the line
Drive.Files.insert({ title: attachment.getName(), mimeType: attachment.getContentType(), parents: [{ id: folderId }] }, attachment.copyBlob()); with title: attachment.getName()
This means that you are creating a file on your Drive with the title that is exactly the same like the original name of the attachment
If instead you would like to give the file an assigned name plus the current date - you need to define the assigned name and get the current timestamp
For this, you can replace the line above by the following three lines:
var myName = "project";
var currentDate = new Date();
Drive.Files.insert({ title: myName + " " + currentDate, mimeType: attachment.getContentType(), parents: [{ id: folderId }] }, attachment.copyBlob()); ` with `title: attachment.getName()
Use new Date() to retrieve the current timestamp
If you would like to fromat the timestamp retrieved by new Date() - use Utilities.formatDate(date, timeZone, format)

Papa Parse first header field rendered with quotes

I am running papa parse on the server to read and parse a csv file usign the following code:
function getData() {
console.log("Started getData...");
const file = fs.createReadStream(filePath);
papa.parse(file, {
header: true,
complete: createRecord
});
}
My complete callback function looks like this:
function createRecord(row, file) {
records = row.data;
console.log("Started createRecord...");
records.forEach((record) => {
console.log(record)
Activity.create(record);
});
}
I then write this to a MondoDB database. Everything works perfectly well, except for the first field name that is parsed by papa. The record output from the console.log(record) call above looks like this:
{
'Activity_Date': '2018-08-28',
Time_Start: '2018-08-28 20:20',
Time_End: '2018-08-28 21:01',
Duration: 0.027962963,
Subject: 'CS410',
Semester: 'Fall 2018',
Week: 1,
Task: 'Lectures',
Day: 2
}
Notice the quotes around Activity_Date. This causes Mongo to ignore the first field and only commit the remainder to the database.
Attempting to edit the csv file differently has not resulted in anything useful. What is weird is that if I look at the meta data from papa parse, it gives me the field names without the qoutes.
The Mongo Schema for Activity looks like this:
const mongoose = require("mongoose");
const activitySchema = new mongoose.Schema({
Activity_Date: String,
Time_Start: String,
Time_End: String,
Duration: Number,
Subject: String,
Semester: String,
Week: Number,
Task: String,
Day: Number
});
module.exports = mongoose.model("Activity", activitySchema)
Any assistance in getting rid of the quotes will be very welcome!
Thanks
It has been a while since asked but i will leave what worked for me. Try using this in the config object:
transformHeader: h => h.trim()

Aurelia converted values and dataTables

I'm using DataTables jQuery plugin in Aurelia component. using column ordering and it works well excluding columns with dates.
Inside this columns I'm using value-convertet to convert isoString value to DD.MM.YYYY date format. Value covreters usage leads to wrong date column ordering, but if I'm not using value-converter everything works well. Unfortunately I didn't find any reason why it doesn't work correctly.
Wrong filtering example: I see rows with date value like 27.05.2010 before 18.05.2017
DataTables init:
$('#searchResultsTable').dataTable({
destroy: true,
searching: false,
paging: false,
orderMulti: false,
order: [[ 2, "desc" ]],
dateFormat: 'DD.MM.YYYY'
});
Date value converter (using moment library):
import * as moment from 'moment';
export class DateFormatValueConverter {
toView(value: Date, format: string): string {
if (value) {
return moment(value).format(format);
}
return null;
}
fromView(value: string, format: string): Date {
var isValid = moment(value, format, true).isValid();
if (value && isValid) {
return moment(value, format).toDate();
}
return null;
}
}
UPDATE:
Ordered with value converter
Orderd without ValueConverter(ordered like it should 2017 year value on the top)
The ordering mechanism of the data table is working correctly - it's your understanding that's off I'm afraid.
When ordering in descending order, any that start with 27. will be at the top, as they're the "biggest". Within all the dates that start with 27, it'll order on the month, biggest first, and then the year.
The order mechanism doesn't realise you're ordering a date so we need to look at the Custom Sorting Plugins;
https://www.datatables.net/plug-ins/sorting/
And specifically the Date-De plugin - as that matches your date format;
https://www.datatables.net/plug-ins/sorting/date-de
An example taken from the above page;
$('#example').dataTable( {
columnDefs: [
{ type: 'de_datetime', targets: 0 },
{ type: 'de_date', targets: 1 }
]
});

What is the proper way to format a UNIX timestamp into a human date in Kendo UI Grid?

Well there seems to be a multitude of similar questions, but none that I can find answering this specific question.. so here goes..
Have a working Kendo UI grid. My datasource is returning a timestamp - here's the JSON response coming back to the code:
You'll notice that the next line is also a date.. returned by MySQL as a standard DateTime format - which I would be happy to use directly. But I've converted the date to a timestamp which I thought would be more universal. (??)
Now I need to do two things - format the timestamp into a readable date and edit the date so it can be saved back to the datasource. But let's tackle formatting first.
My code to display the column currently looks like this:
{ title: "Trial<br>Date",
field: "customer_master_converted_to_customer_date",
format: "{0:d/M/yyyy}",
attributes: {
style: "text-align: center; font-size: 14px;"
},
filterable: true,
headerAttributes: {
style: "font-weight: bold; font-size: 14px;"
}
},
Although I've tried..
toString(customer_master_converted_to_customer_date, "MM/dd/yyyy")
.. and several variations of that - in terms of format string. And yes, I've tried entering:
type: "date",
No matter what I do, I only get the timestamp.
Anyone?
You need to convert the timestamp to a JavaScript date first. Here is a sample implementation:
$("#grid").kendoGrid({
dataSource: {
data: [
{ date: 1371848019 }
],
schema: {
model: {
fields: {
date: {
type: "date",
parse: function(value) {
return new Date(value * 1000);
}
}
}
}
}
}
});
Here is it live: http://jsbin.com/utonil/1/edit
I just had the same problem and i tried this and now works perfectly, good luck.
template :#= kendo.toString(new Date(parseInt(dateOfBirth)), 'yyyy-MM-dd')#"
whre dateOfBirth is the date to format, the result will be like this : 2015-09-11.
good luck.
Thank you #Atanas Korchev, that worked for me, here is what I ended up doing:
// in datasource
schema:{
model: {
fields: {
date: { type: "date",
parse: function(value) {
return new Date(value);
}
}, // … other fields
// in columns
columns: [
{
field: "date",
title: "Date",
type: "date",
format: "{0:dd MMM yyyy}"
},
// ... other columns
]
The easiest way to use the TimeStamp format data from your database for KendoGrid.
https://stackoverflow.com/a/67106362/5671943
<kendo-grid-column class="CreatedAt" field="CreatedAt" title="title"
[width]="120" [headerStyle]="{'background-color': '#36455a','color': '#fff'}"
[style]="{'background-color': '#36455a','color': '#fff'}">
<ng-template kendoGridCellTemplate let-dataItem>
{{dataItem.CreatedAt | date:"yyyy/MM/dd HH:mm:ss"}}
</ng-template>
</kendo-grid-column>

How can I access a date typed field from an ExtJS JSonStore?

I've been trying to retrieve a date value and an integer value from the database, using the following code:
var l_alsChampsMois, l_stoDonneesMois;
try {
l_alsChampsMois = [
{name: "date_mois", type: "date", dateFormat: "Y-m-d"},
{name: "indice", type: "integer"}
];
l_stoDonneesMois = new Ext.data.JsonStore({
fields: l_alsChampsMois,
autoLoad: false,
proxy: {
type: "ajax",
url: "/commun/req_sql/req_ind_per_mois.php",
reader: {
type: "json",
root: "rows"
},
// some configs to use jsFiddle echo service (you will remove them)
actionMethods: {
read: "POST"
},
extraParams: {
key:"test"
}
},
listeners: {
load: function(objStore, alsLignes, blnOk, objOptions) {
window.alert("Mois fin : " + objStore.getAt(0).get("date_mois"));
}
}
});
l_stoDonneesMois.load({params: {
p_idsoc: l_strIdSociete,
p_mois: l_datDebut.getMonth() + 1,
// getMonth renvoie 0 pour janvier, etc.
p_annee: l_datDebut.getFullYear(),
p_debut: 1,
p_etape: 1
}
});
with l_strIdSociete and l_datDebut being variables previously assigned and /commun/req_sql/req_ind_per_mois.php the PHP page that retrieves the data and converts it to JSON.
It seems to work fine (indeed, Firebug tells me the load does retrieve a data structure with "date_mois" and "indice" containing the values I expect them to), only the window.alert returns undefined. If I replace "date_mois" with "indice", it returns the expected value for "indice".
I've tried to use objStore.getAt(0).getData()["date_mois"], to no avail.
My only clue about this is that "date_mois" in the data structure shown by Firebug is an Object, but even so it shouldn't be undefined, now should it? I looked up http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Field-cfg-type that wasn't exactly forthcoming with straight answers.
So what did I do wrong there?
If you need current time you can use php time function(note: it returns seconds, JS uses milliseconds), in another case you need to convert by mktime function.