Year pick from vuejs date picker vue-date-pick - vue.js

Hi i want to pick year in vue-date-pick
<date-pick
v-model="award.date"
:allowed-dates="allowedYears"
type="year"
>
</date-pick>
allowedYears: date => {
return parseInt(date, 10) % 2 === 0;
},
https://dbrekalo.github.io/vue-date-pick/

Related

Retrieving product count for each month in the current year laravel

I'm trying to count the number of orders for each month of the current year as follows
//month arrays to loop through
$month = ['1','2','3','4','5','6', '7', '8', '9', '10', '11','12'];
//current year
$year = Carbon::now()->year;
//variable to store each order coubnt as array.
$new_orders_count = [];
//Looping through the month array to get count for each month in the provided year
foreach ($month as $key => $value) {
$new_orders_count[] = Order::whereYear('updated_at', $year)
->whereMonth('updated_at',$value)
->where(['orders.status_id'=>11])
->orWhere(['orders.status_id'=>14])
->orWhere(['orders.status_id'=>4])->count();
}
This gives me the following result
Note that this is returning 48 even where it should return zero or a different value from 48, for example, months 6 through to 12 have no data thus I expect the query to return zero but it is not. Why is this and how can I achieve what I need? I'm using laravel's default timestamp format for the created_at and updated_at fields
//current year
$year = Carbon::now()->year;
//variable to store each order count as array.
$new_orders_count = [];
//Looping through the month array to get count for each month in the provided year
for($i = 1; $i <= 12; $i++){
$new_orders_count[] = Order::whereYear('updated_at', $year)
->whereMonth('updated_at', $i)
->where(function ($query) {
$query->whereIn('orders.status_id', [11, 14, 4]);
})->count();
}
Try like this way. change your query
Order::whereYear('updated_at', $year)
->whereMonth('updated_at', $value)
->where(function ($query) {
$query->whereIn('orders.status_id', [11, 14, 4]);
})->count();
may this help ...

Formatted date for vue-date-pick is 1 day bigger

In my vue/cli 4/vuex / bootstrap-vue project app I install date picker with custom format, like here
https://dbrekalo.github.io/vue-date-pick/examples.html#custom-date-parser
I use moment/moment-timezone in my project, so I do not want to use fecha as in example
I have to convert date value from mysql to datepicker format and I have a problem that my converted date for
datepicker is 1 day bigger...
I have in my component:
<date-pick
v-model="editableAd.expire_date_formatted"
:format="date_picker_format"
:parseDate="parseDate"
:formatDate="formatDate"
:inputAttributes="{size: 32}"
></date-pick>
...
import moment from 'moment-timezone'
console.log('settingsTimeZone::')
console.log(settingsTimeZone) // it shows Europe/Kiev
moment.tz.setDefault(settingsTimeZone)
...
date_picker_format: 'Do MMMM, YYYY',
...
// setting formated date for dapicker
this.editableAd.expire_date_formatted = this.formatDate(this.editableAd.expire_date, this.date_picker_format)
...
formatDate(dateObj) {
console.log('typeof dateObj::')
console.log(typeof dateObj)
console.log(dateObj) // it has ‘2023-01-19’ value
if (typeof dateObj === 'string') {
dateObj = moment(dateObj, this.date_picker_format)
}
console.log('++typeof dateObj::')
console.log(typeof dateObj)
console.log(dateObj)
console.log('RESULT moment(dateObj).format(this.date_picker_format)::')
console.log(moment(dateObj).format(this.date_picker_format)) // BUT it has ‘20th January, 2023’ value
return moment(dateObj).format(this.date_picker_format) // returns invalid ‘20th January, 2023’ value which I see in datepicker
What I see in console for dateObj var : https://imgur.com/a/KZLtXiL
"bootstrap-vue": "^2.1.0",
"font-awesome": "^4.7.0",
"moment": "^2.24.0",
"moment-timezone": "^0.5.27",
"vue": "^2.6.10",
"vue-date-pick": "^1.2.1",
Why error and how it can be fixed?
MODIFIED BLOCK 2:
I removed moment.tz from my project.
In /etc/php/7.2/apache2/php.ini I changed
Timezone ='Europe/Uzhgorod' // That it near my place I live
date.timezone = "Europe/Uzhgorod"
So my phpinfo has this output:
"Olson" Timezone Database Version 0.system
Timezone Database internal
Default timezone Europe/Uzhgorod
I searched how to get a timezone from client browser and found this
get client time zone from browser branch and checking :
var timedifference = new Date().getTimezoneOffset();
console.log('timedifference::')
console.log(timedifference)
var rightNow = new Date();
var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);
var temp = jan1.toGMTString();
var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ") - 1));
var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);
console.log('std_time_offset::')
console.log(std_time_offset)
let jstz = require('jstimezonedetect')
let timezone = jstz.determine()
console.log('timezone::')
console.log(timezone)
console.log('===========================')
I have next output: https://imgur.com/a/84RqqPJ and again running code when I see date is changed +1 day :
console.log('__')
console.log('__')
console.log('typeof dateObj::')
console.log(typeof dateObj)
console.log(dateObj)
if (typeof dateObj === 'string') {
dateObj = moment(dateObj, this.date_picker_format)
}
console.log('++typeof dateObj::')
console.log(typeof dateObj)
console.log(dateObj) // it has ‘2023-01-19’ value
console.log('RESULT moment(dateObj).format(this.date_picker_format)::')
console.log(moment(dateObj).format(this.date_picker_format)) // BUT it has ‘20th January, 2023’ value
return moment(dateObj).format(this.date_picker_format)
and what I see in console: https://imgur.com/a/Y1aSBez
That is strange that .d has 20 day. What is it? Some day zero based option?
The problem you're having is due to some automatic localization formatting. I too don't get why does this happens, since we are providing only dates, they should be treated as such regardless of our timezone. But to solve this issue we have to read it as UTC with moment.utc() and then format it using your desired format.
From vue-date-pick docs example I've worked out a solution using moment.
var app = new Vue({
el: '#app',
components: {
'date-pick': VueDatePick,
},
data: () => ({
format: 'Do MMMM, YYYY',
date: '19th January, 2023',
}),
methods: {
parseDate(value) {
const m = moment.utc(value, this.format);
return new Date(m.year(), m.month(), m.date());
},
formatDate(value) {
return moment.utc(value).format(this.format);
}
}
});
<link href="https://cdn.jsdelivr.net/npm/vue-date-pick#1.2.1/dist/vueDatePick.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-date-pick#1.2.1/dist/vueDatePick.min.js"></script>
<script src="https://momentjs.com/downloads/moment.min.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone.min.js"></script>
<div id='app'>
<date-pick v-model="date" :format="format" :parse-date="parseDate" :format-date="formatDate"></date-pick>
</div>

select, group, sum with ASP LINQ + private compare method

i am working on an ASP MVC Project and i have a part where i need to export some data to an excel file. the thing is that i need to sum some rows to display a single row. I am not familiar with sql or linq and i am struggling to get the result i want.
there should be 4 columns: requester(string), date(datetime), collection(string), timeinvested(int).
the grouping should be by the column 'collection' (string) as default and if the filter input for requester or date was filled than that would be the next level of grouping. the column 'timespent' should be summarized by the result of the grouping.
example:
requester, date, collection, timeinvested
(1) john, jan 1st, 2019, collection1, 1
(2) mike, jan 1st, 2019, collection1, 3
(3) eric, jan 1st, 2019, collection1, 2
(4) july, jan 1st, 2019, collection2, 5
(5) john, jan 1st, 2012, collection1, 3
here we have 5 rows from the table, once we filter to export only by default (column collection) then rows 1+2+3+5 should sum to 1 row and the 2nd row will be row 4, because the collections are different. like so:
requester, date, collection, timeinvested
(1) john, jan 1st, 2019, collection1, 9
(2) july, jan 1st, 2019, collection2, 5
if i choose to filter also by requester or date then it should apply accordingly.
one big thing here is that there is a private method that checks the date to a particular date and it should export to the file the date year if its after or before the checked date. for example: relative date dec 1st of the same year
if the row date is before relative date then we should write date year - 1;
thanks
var q = (from a in items
select new {
Requester = a.Requester.Name,
Collection = a.Collection.Name,
TimeSpent = a.TimeSpent,
Year = a.Date,
})
.ToList()
.GroupBy(x => new {
x.Requester,
x.Collection,
x.Year,
x.TimeSpent
})
.Select(y => new CollectionModel {
Requester = y.Key.Requester,
Collection = y.Key.Collection,
TimeSpent = y.Sum(z => Convert.ToInt32(z.TimeSpent)),
Year = checkDate(y.Key.Year),
});
return q.ToList();
this is how i made it work:
var q = (from a in items
select new {
Requester = a.Requester.Name,
Collection = a.Collection.Name,
TimeSpent = a.TimeSpent,
Year = a.Date,
})
.Where(Where(a => dt == null || (dt != null && a.Year == dt.Year))
.ToList()
.GroupBy(x => new {
x.Requester,
x.Collection,
x.Year,
})
.Select(y => new CollectionModel {
Requester = y.Key.Requester,
Collection = y.Key.Collection,
TimeSpent = y.Sum(z => Convert.ToInt32(z.TimeSpent)),
Year = checkDate(y.Key.Year),
});
return q.ToList();
First, remove the "x.TimeSpent" from your GroupBy, you need to aggregate that variable, not group by it. secondly, could you post the output as well?

Date comparision on cshtml view

I am using the below code to compare 2 dates on cshtml view with knockout binding.
data-bind="visible: (new Date(appointmentDate) - new Date() < 0) && isStart()"
It is working fine but that is including time as well while comparing. I don't want to include time in comparision only date.
I quick search on google pointed me to Formatting Date in Knockout Template this will allow us to get the date and compare it. Looking like
data-bind="visible: (
moment(new Date(appointmentDate)).format('MM/DD/YYYY') -
moment(new Date()) < 0) && isStart()"
I didn't try just let me know if works
Also momento allows you to calculate difference of dates
var dateB = moment('2014-11-11');
var dateC = moment('2014-10-11');
console.log('Difference is ', dateB.diff(dateC), 'milliseconds');
console.log('Difference is ', dateB.diff(dateC, 'days'), 'days');
console.log('Difference is ', dateB.diff(dateC, 'months'), 'months');
So basically we would do
data-bind="visible: (
moment(new Date(appointmentDate)).format('MM/DD/YYYY').diff(new Date().format('MM/DD/YYYY'),'days') < 0) && isStart()"

SQL group dates as week date

I have a table which contains func_id and date in format like (07.06.14 ). I am trying to create a function that returns the number of dates that is the week day of a date (07.06.14 ) and the func_id will equals the func_id that I will write. For example:
My table:
func_id 11 / date = 07.06.14
That will return 1 from 0. (Saturday code = 0);
I tried:
function Get_weekday($data){
$dat = explode(".",$data);
$date = date("l", mktime(0, 0, 0, $dat[0] , $dat[1], $dat[2]));
if($date == "Monday"){return 4;}
if($date == "Tuesday"){return 3;}
if($date == "Wednesday"){return 2;}
if($date == "Thursday"){return 1;}
if($date == "Friday"){return 0;}
if($date == "Saturday"){return 0;}
if($date == "Sunday"){return 0;}
}
function count_per_date($func_id,$day){
$result = mysql_query("SELECT COUNT(date) as data,date FROM entregas WHERE func_id = '$func_id' GROUP BY date");
while($row = mysql_fetch_assoc($result))
{
//That part I dont really know what to do
}}
Sorry for my english, I dont speak very well ):
You're looking for DAYOFWEEK
Example
SELECT DAYOFWEEK('2007-02-03');
Further info : DAYOFWEEK documentation