I'm trying to show todays date in flatpickr on open.
But it won't work.
This is what i tried:
onOpen: function(selectedDates, dateStr, instance) {
instance.set('defaultDate', 'today');
},
Related
how can you make a query in Prism to filter data by date?
For example, I would like to take data from the last week or month
return await prisma.orders.findMany({
where: {
date: {
},
},
});
I don't really know what to do after the date..
How could I filter in this light?
For example, data from the last week and the last month
You can do something like this to filter between a date range, you can get today's date and the date of last week/month and get the records through lte and gte operators.
return await prisma.orders.findMany({
where: {
date: {
lte: 2022-10-30,
gte: 2022-10-15,
},
},
});
Here is an example from Prisma Documentation
I want to update the current date in yyy-mm-dd form so i can query it later but i m unable to insert date in the required format.here what i had did till
const sampleSchema = new mongoose.Schema({
username:String,
date:{
type:Date
}
});
const Sample = new mongoose.model("Sample", sampleSchema);
let currentDate=new Date
const sample = new Sample({
username:req.body.username,
date:currentDate.toISOString().split("T")[0]
});
sample.save()
I'm getting this
{
"_id": {
"$oid": "61b5862c6ff7ff297ae97d0c"
},
"user": "username",
"date": {
"$date": "2021-12-12T00:00:00.000Z"
},
"__v": 0
}
I want date in yyy-mm-dd format.
Use moment.js to convert the ISO formatted date into normal formatted date YYYY-MM-DD Don't try to store your own formate of date on MongoDB because it creates problems in querying data based on date and time. So update it into ISO and then convert it into different formate at the backend
Converting ISO formate Date into YYYY MM DD
date = new Date('2013-08-03T02:00:00Z');
date = moment(date).format('YYYY MM DD'); //result 2013 08 03
Converting YYYY MM DD formate into IOS formate
date = new Date('2013-08-03');
date = moment(date).format();
Another thing users can do is to store data in the form of a string
and use $gt, $gte, $lte and $lt operators for queries
I am creating a Node js project which connects to an API and returns a date in the format of 2021-02-28T22:46:01.000-0500 I would like to convert this date to a format of mm/dd/yyyy format. Even better if its mm/dd/yyy hh:mm:ss
This is the current line of code.
this.createdDate = djItem.fields.created
What do I need to add to update the date format?
I am trying to create a function which can be used by two different lines of codes with dates.
this.createdDate = getDate(djItem.fields.created ? djItem.fields.created : 0);
getDate = function(date){
const dateAsNumber = date.parse(createdDate)
console.log(dateAsNumber)
const dateOptions = {
month: "numeric",
day: "numeric",
year: "2-digit",
hour: "numeric",
minute: "numeric",
second:"numeric",
}
const newDateString = new Date(dateAsNumber).toLocaleDateString("us-en", dateOptions);
console.log(newDateString)
}
Current error is createdDate is not defined
You can do this by first parsing the date time format (this will give you a timestamp as a bigint) and then by using the built in data function .toLocaleDateString() function with options that meet your needs.
const dateAsNumber = Date.parse("2021-02-28T22:46:01.000-0500")
// it is now a number
console.log(dateAsNumber)
const dateOptions ={
month: "numeric",
day: "numeric",
year: "2-digit",
hour: "numeric",
minute: "numeric",
second:"numeric",
}
const newDateString = new Date(dateAsNumber).toLocaleDateString("us-en", dateOptions);
console.log(newDateString)
So the problem I'm currently having is :
I've a list of gym classes, which every class has an OpeningTime. I want to fetch all the classes from the current day, but. I'm not getting the same result locally and in production, cause for some reason, when Im deploying my backend to Heroku, the timezone is being setted by default to UTC (when I've a GMT-3 timezone).
Here is my query :
var now = moment();
var startOfDay = now.startOf('day').format();
var endOfDay = now.endOf('day').format();
var clasesDB = await Clase.find({ $and: [{ openingTime: { $gte: startOfDay } }, { openingTime: { $lte: endOfDay } }] })
So, like I said before, the problem is ocurring when, for example:
When I fetch the classes at my local time (Ex: 02-26-19 21:00hs ( GMT-3) ), the queries are actually showing the classes from 02-27, because, at MINE 21:00hs, on the server side is already 00:00, so, the date is not 02-26 anymore. And I dont want this kind of output.
How can I get a workaround to solve this?
Thanks in advance! :)
Don't use .format(), this makes a string. Compare directly Date values, i.e. use
var now = moment();
var startOfDay = now.startOf('day').toDate();
var endOfDay = now.endOf('day').toDate();
By default moment uses local times, so moment().startOf('day') returns midnight of local time. If you want to get midnight of UTC then use moment.utc().startOf('day').
If you don't rely on "local" time zone, then you can specify it like moment.tz("America/New_York").startOf('day')
No matter which time you need, never compare Date values by string, use always actual Date value.
By default in MongoDB a date field is stored in UTC, You can create a date with offset while writing to store it in your timeZone. But you've not done it while writing documents then you need to convert date field to your required timeZone while reading data from database. Check this below example.
JavaScript Code :
const today = new Date().toLocaleDateString(undefined, {
day: '2-digit',
month: '2-digit',
year: 'numeric'
}) // 02/26/2020. If your Heroic server is in different location then specify your locale in place of undefined - whereas undefined picks local time by default. Ex:- 'en-Us' for USA
Query :
db.collection.aggregate([
{ $addFields: { timewithOffsetNY: { $dateToString: { format: "%m/%d/%Y", date: "$openingTime", timezone: "America/New_York" } } } },
{ $match: { timewithOffsetNY: today } }, { $project: { timewithOffsetNY: 0 } }
])
Above query is written for New York timeZone, you can convert it according to your timeZone, In test url you can see 1st doc is not returned though it is on 2020-02-26 cause offset to New York as of today is 5hrs, So after converting date becomes as 2020-02-25.
Test : MongoDB-Playground
Ref : $dateToString
I use in my grids the extension yii2-export to export data to XLS and XLSX. Whatever I try, Excel does not automatically recognize the dates. I have tried e.g. the following columns formats:
'mydate', // yyyy-mm-dd
'mydate:date', // dd.mm.yyyy
[
'attribute' => 'mydate',
'value' => function($model) {
return date("m/d/Y", strtotime($model->mydate));
},
], // mm/dd/yyyy
I suppose, I have to mark the dates as date. But how?
You can try this:
In grid configuration add:
'onRenderDataCell' => function ($cell, $content, $model, $key, $index, $grid) {
if ($key == 'mydate') {
$cell->setValue(\PHPExcel_Shared_Date::PHPToExcel(\Yii::$app->formatter->asTimestamp($model->$key)));
}
},
Absolutely not 100% sure if it works. Some namespaces fixing might be needed as well. The idea is to change the content of the cell, I was checking possible options from PHPExcel repo but I have not verified this.
Few links from it has been taken:
Export menu docs #40
PHPExcel_Shared_Date class
PHPExcel example file with different types
In your model you should provide the date like following:
public function getDate(){
return \PHPExcel_Shared_Date::PHPToExcel(Yii::$app->formatter->asTimestamp($this->date));
}
Your excel export contains the normal columns and a formatting function for the specific column.
<?= ExportMenu::widget([
'onRenderSheet' => function($sheet, $grid){
$sheet->getStyle('What ever column it is(A, B, C, ..)')->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_DDMMYYYY);
}
},
'columns' => [
[
'attribute' => 'date',
'label' => 'Datum',
],
]);?>`