I am using DatePicker of NativeBase and want to change the format of the displayed date after picking a date. I am unable to find a relevant prop due to lack of docs.
Is there a way I could change the format of the date as in DatePickerAndroid using format="YYYY-MM-DD"?
Fixed in native-base v2.6.1 onwards.
<DatePicker
formatChosenDate={date => {return moment(date).format('YYYY-MM-DD');}}
..... />
Try this:----
async onPressAction() {
try {
const {action, year, month, day} = await DatePickerAndroid.open({
// Use `new Date()` for current date.
// May 25 2020. Month 0 is January.
date: new Date(2018, 6, 26)
});
if (action !== DatePickerAndroid.dismissedAction) {
// Selected year, month (0-11), day
var date = new Date(year, month, day);
var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate();
console.log('>>>>>>>>>>>>>'+year);
console.log('>>>>>>>>>>>>>'+month);
console.log('>>>>>>>>>>>>>'+day);
console.log(year+'-'+month+'-'+day);
}
} catch ({code, message}) {
console.warn('Cannot open date picker', message);
}
}
Related
How do I save a timestamp value to firestore using cloud functions.
I tried this (adding 30 days to the timestamp) but it ends up saving the value as a number
var now = new Date();
await docRef.update({
'subscription':{
package:req.query.package,
endDate: now.setTime(now.getTime()+(30 *24+60+60+1000))
}
})
If you want to add days to the date, then you should setDate() instead. By using setTime() you're setting the time instead of the date. See code below:
var now = new Date();
now.setDate(now.getDate() + 30);
await docRef.update({
'subscription':{
package: req.query.package,
endDate: now
}
})
For more information, you should check out this documentations:
Date.prototype.setDate()
Date.prototype.getDate()
I have a mongodb model whose creation date is stored in milliseconds(epoch time). I want to fetch only documents whose milliseconds matches today.
So, here is what I'm trying to achieve:
let query = {"pick_up_time":12262672627271}; // I want this to translate to pick_up_time is today
Is it possible to apply a transformation function to this?
I have this function to check if requested date is today
function isToday(someDate) {
const today = new Date()
return someDate.getDate() == today.getDate() &&
someDate.getMonth() == today.getMonth() &&
someDate.getFullYear() == today.getFullYear()
}
So, that my query will now be
let query = {"pick_up_time":isToday(12262672627271)};
How can something like this be achieved with mongodb?
I think this function can help you to create dynamic queries for each day
const getTimeQuery = () => {
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth();
const day = now.getDate();
const startDate = new Date(year, month, day, 0, 0, 0).getTime();
const endDate = new Date(year, month, day, 24, 0, 0).getTime();
const query = {
"pick_up_time": { $gte: startDate, $lte: endDate }
}
return query
}
console.log(getTimeQuery());
I'm a beginner with d3.js. The goal for now is to take the date column from "2018-11-01" to "Nov 18".
The dates start from "2016-11-01" to "2020-08-01" and each date in between is only the first of each month.
For example:
"2016-11-01",
"2016-12-01",
"2017-01-01",
"2017-02-01",
"2017-03-01",
"2017-04-01",
"2017-05-01",
The code I've used is below:
var format = d3.timeFormat("%b %Y");
var data = d3.dsv(",", dsvPath, function (d) {
return {
//Year : getYear(new Date(+d.year,0,1)),
date: format(new Date(d.date)),
['Catan=count']:+d['Catan=count'],
['Dominion=count']:+d['Dominion=count'],
['Codenames=count']:+d['Codenames=count'],
['Terraforming Mars=count']:+d['Terraforming Mars=count'],
['Gloomhaven=count']:+d['Gloomhaven=count'],
['Magic: The Gathering=count']:+d['Magic: The Gathering=count'],
['Dixit=count']:+d['Dixit=count'],
['Monopoly=count']:+d['Monopoly=count']
//['Running Total'] : +d["running_total"]
};
}).then(function (data) {
console.log(data[0]);
minDate = d3.min(data,function(d){console.log(d); return d.date;});
maxDate = d3.max(data,function(d){console.log(d); return d.date;});
console.log(minDate);
However, for the minimum date, I get April 2017 instead of November 2016. The max date is May 2020 instead of September 2020.
When I read in the data as is without the data formatting, the minDate and maxDate are correct. As soon as I format, however, Oct 2016, which doesn't exist in the dataset, is somehow logged twice.
Furthermore, the data in the console stops at "July 2020": there's no August and Sept 2020. I'm honestly very puzzled.
Any help would be much appreciated!
Thank you!
Converting to a Date via the Date() constructor is notoriously tricky. Thankfully, since D3 gives us methods to go from a date to a string, it also has the reverse, going from a string to a date.
Our process will be twofold:
For each string in a certain format, convert to Date with d3.timeParse().
Convert each date to your desired string format.
(1.) (2.)
"2016-01-01" 🡒 Date<2016-01-01T00:0O> 🡒 "January 2016"
d3.timeParse d3.timeFormat
You already have (2.) in your code, so we need (1.):
var toDate = d3.timeParse("%Y-%M-%d");
var format = d3.timeFormat("%b %Y"); // (2.)
var data = d3.dsv(",", dsvPath, function (d) {
return {
date: format(toDate(d.date)),
};
})
I'm using bootstrap datepicker.
I'm setting the minimum date to the current date and the max date to December of next year.
minDate = new Date();
minDate.setDate(1);
maxDate = new Date();
maxDate.setFullYear(maxDate.getFullYear() + 1);
maxDate.setMonth(12);
maxDate.setDate(0);
maxDate.setHours(23,59,59);
$('#startDateInput').datepicker({
format : "MM yyyy",
viewMode : "months",
minViewMode : "months",
startDate : minDate,
endDate : maxDate
});
$('#endDateInput').datepicker({
format : "MM yyyy",
viewMode : "months",
minViewMode : "months",
startDate : minDate,
endDate : maxDate
});
I'm reading the startdate and end date from the datepicker as below:
var startDateString = $('#startDateInput').val();
var startParts = startDateString.split(' ');
var startEpoch = new Date(parseInt(startParts[1]) , monthsArray.indexOf(startParts[0].toLowerCase()), 01).getTime();
document.getElementById('startDate').value = startEpoch;
var endDateString = $('#endDateInput').val();
var endParts = endDateString.split(' ');
var endEpoch = new Date(parseInt(endParts[1]) , monthsArray.indexOf(endParts[0].toLowerCase())+1,0,23,59,59).getTime();
I store the epochs in the database. When I retrieve the dates and show it in the edit form, I'm doing this
if ($('#object').val()) {
var stDate = new Date(Number("${object.startDate}"));
var enDate = new Date(Number("${object.endDate}"))
$('#startDateInput') .datepicker('setDate', new Date(stDate));
$('#endDateInput').datepicker('setDate', new Date(enDate));
}
Let's say I've selected dec 2019 as input today and saved it. Later when I edit the same form, the endDateInput is not filled because the end date is not inclusive, I suspect.
I use Bootstrap date paginator. I really can't figure out by myself how to enable datepicker to allow select a date. Calendar is shown but the date selection is disabled.
The initialization code is.
var options = {
selectedDate: $('#selected-date').html(),
selectedDateFormat: 'YYYYMMDD',
onSelectedDateChanged: function (event, date) {
$('#selected-date').html(moment(date).format('YYYYMMDD'));
disableDatePaginator();
$('#log-table').DataTable().ajax.reload(function(json){ enableDatePaginator(); });
},
};
$('#date-paginator').datepaginator(options);
I tried $('#date-paginator').datepaginator(options).datepicker(); with no success.
Finally I found the reason of the issue. It's necessary to specify valid startDate and endDate in options for date paginator like this.
var options = {
startDate: moment(new Date(2000, 1, 1)),
endDate: moment(new Date(2100, 1, 1)),
selectedDate: $('#selected-date').html(),
selectedDateFormat: '{{date_format}}',
onSelectedDateChanged: function (event, date) {
$('#selected-date').html(moment(date).format('{{date_format}}'));
disableDatePaginator();
$('#log-table').DataTable().ajax.reload(function(json){ enableDatePaginator(); });
},
};