Change date storage format in MongoDB - mongodb

In an input json file i receive dates in this format:
{ "dt_received" : "2016-01-22T12:35:52.123+05" }
When loaded into MongoDB, those dates are stored this way:
dt_received: "2016-01-22T07:35:52.123Z"
The issue is that i need the timezone to calculate my indicator.
In constraint, i can't create new columns such as "dt_received_timezone".
So i'm looking for changing the date storage format into MongoDB in order to make the timezone appear (or at least not disapear)
Is it a way to to this? Or any solution ?

If you receive data from various time zones and want to keep the time zone offset, you will have to save it into the database like this:
var now = new Date();
db.data.save( { date: now, offset: now.getTimezoneOffset() } );
You can then reconstruct the original time like this
var record = db.data.findOne();
var localNow = new Date( record.date.getTime() - ( record.offset * 60000 ) );
See the documentation for further details

Related

DateRangePicker - Grey out unavailable dates

As the title suggests I am using DateRangePicker to add date ranges to an array. If possible I would like to be able to "grey" out already selected dates in the array. Is there anyway to do this?
Here is the solution to returning the dates in-between the range in case anyone else needs it.
List<DateTime> getDaysInBetweenIncludingStartEndDate(
{required DateTime startDateTime, required DateTime endDateTime}) {
// Converting dates provided to UTC
// So that all things like DST don't affect subtraction and addition on dates
DateTime startDateInUTC =
DateTime.utc(startDateTime.year, startDateTime.month, startDateTime.day);
DateTime endDateInUTC =
DateTime.utc(endDateTime.year, endDateTime.month, endDateTime.day);
// Created a list to hold all dates
List<DateTime> daysInFormat = [];
// Starting a loop with the initial value as the Start Date
// With an increment of 1 day on each loop
// With condition current value of loop is smaller than or same as end date
for (DateTime i = startDateInUTC;
i.isBefore(endDateInUTC) || i.isAtSameMomentAs(endDateInUTC);
i = i.add(const Duration(days: 1))) {
// Converting back UTC date to Local date if it was local before
// Or keeping in UTC format if it was UTC
if (startDateTime.isUtc) {
daysInFormat.add(i);
} else {
daysInFormat.add(DateTime(i.year, i.month, i.day));
}
}
return daysInFormat;
}
Yes, you can send disabled dates into the component.
Check this sample of the documentation.
For further options, check the whole docs.

Change input Date format? Vuejs + mongoose

Problem: Change the Date input field from "mm/dd/yyyy" to "dd/mm/yyyy".
I already know how to change after i receive the date, but the problem is that when the client is typing the input is still receiving "mm/dd/yyyy".
My mongoose schema:
const schemaRegister = new mongoose.Schema({
date: Date,
});
My input area:
<b-form-input v-mask="'##/##/####'" v-model="date"></b-form-input>
My date formating (using momentsjs):
changeDateFormat() {
let fixedDate = moment(this.registers[i].date).format("L");
this.registers[i].date = fixedDate;
}
I am displaying the 'fixedDate' on the table, but it doesn't help a lot because when the client is typing he thinks the first 2 slots are the days (dd), but in reality they are the month (mm). As a solution i thought of using the Date as a String but then it would make the verification very difficult.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
just pass the parameters in the correct order, like this:
new Date(day, monthIndex, year);
I wasn't using the 'momentsjs' correctly, first i needed to parse the input date by using
let formatedDate = moment(this.date,"DD-MM-YYYY");
and then for displaying the date i should have used
let fixedDate = moment(this.registers.date).format("DD/MM/YYYY");

Construct Mongo ObjectId from timestamp with unique increment

I want to generate mongo objectid for the documents to be inserted as new with the timestamp value overwritten. so i used below code to get objectid.
var oIdWithTimestamp = function (timestamp) {
// Convert string date to Date object (otherwise assume timestamp is a date)
if (typeof (timestamp) == 'string') {
timestamp = new Date(timestamp);
}
// Convert date object to hex seconds since Unix epoch
var hexSeconds = Math.floor(timestamp / 1000).toString(16);
// Create an ObjectId with that hex timestamp
var constructedObjectId = mongoose.Types.ObjectId(hexSeconds + "0000000000000000");
return constructedObjectId
};
but if i want to insert 2 documents with same timestamp it doesn't fullfill the need. I noticed there is a get_inc function used to add incrementor value to objectids. And 16777214 different objectids can be generated using same timestamp. Any help regarding how to use this incrementor to get unique timestamp upto 16777214 is appreciated.
I have tried generating random mongo objectid using below snippet.
var bson = require('bson');
var generateObjIdFromTime = function(spefictime) {
spefictime = ~~(spefictime/1000);
return bson.ObjectID(spefictime);
}
It generates random mongo objectids with given timestamp.

How to assign current date to a date field in odoo 10

How to show current date before clicking the date field in odoo?
Odoo Date field class provides methods to get default values for like today.
For dates the method is called context_today() and for datetimes context_timestamp(). You are able to pass a timestamp to this methods to either get today/now (without timestamp) or a timestamp which will be formed by the logged in users timezone.
Code Example:
from odoo import fields, models
class MyModel(models.Model):
_name = 'my.model'
def _default_my_date(self):
return fields.Date.context_today(self)
my_date = fields.Date(string='My Date', default=_default_my_date)
Or the lambda version:
my_date = fields.Date(
string='My Date', default=lambda s: fields.Date.context_today(s))
I found it.It is Simple, just write this on your python code like:
date = fields.Datetime(string="Date", default=lambda *a: datetime.now(),required=True)
or
like this
date = fields.Datetime(string="Date current action", default=lambda *a: datetime.now())
or
like this
date = fields.Date(default=fields.Date.today)

How to use nest in d3 to group using the 'month' as key, but my csv file contains the whole date?

var data = d3.nest()
.key(function(d) { return d.date;})
.rollup(function(d) {
return d3.sum(d, function(g) {return g.something; });
}).entries(csv_data);
using this code above I can group by date which is in the format yyyy-mm-dd , but I want to group using the month as key. How do I do this ?
You can use the builtin method d3.timeMonth() to get the first day of the month for a date like:
d3.nest()
.key(function(d){ return d3.timeMonth(d.date); });
If d.date is not already a javascript Date object you have to first parse it to be one.
var date_format = d3.timeFormat("%Y-%m-%d");
csv_data.forEach(function(d, i) {
d.date = date_format.parse(d.date);
};
You need to change the key function to return the value you want to nest by. In most cases, the key function just returns a property of the data object (like d.date), but in your case the function will require a little more calculation.
If your date is stored as a string of the format "yyyy-mm-dd" you have two options for extracting the month: either
use regular expressions to extract the portion of the string in between the "-" characters, or
convert it to a date object and use date methods to extract a component of the date.
Browsers differ in which date formats they can convert using the native Javascript new Date(string) constructor, so if you're using date objects you may want to use a d3 date format function's format.parse(string) method to be sure of consistent results.