How to calculate days remaining in next Birthday in React using momentJs? - reactjs

I need to calculate the days remaining in next birthday in React using momentJs library. Can somebody provide me the code?

var a = moment([2019, 3, 5]);
var b = moment([2019, 4, 5]);
a.diff(b, 'days') // 30
if you want in single line
let days=moment([2019, 3, 5]).diff(moment([2019, 4, 5]),'days')

i found the right code below:
let daysLeft = ( 'Birthday on ' + moment(date).format("D MMM") + ' (in ' +
moment(moment(date))
.add(
moment(moment().format("YYYY-MM-DD")).diff(moment(date), "years") + 1,
"years"
)
.diff(moment().format("YYYY-MM-DD"), "days") + ' days)');

Related

How to write a 2D array to a single column in Google Sheets, using Google Apps Script?

I'm trying to write the array to a single column, but despite everything seems right to me, it keeps throwing me an error:
Here's the piece of code:
function getGFTickersData() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("LIST OF STOCKS");
var tickerRng = ss.getRange(2, 1, ss.getLastRow(), 1).getValues();
//var TDSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("TickersData");
var TDSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet10");
var tickerArr = [];
for (var b = 0; b < tickerRng.length; b++) {
var tickerToArr = [tickerRng[b]];
if (tickerToArr != '') {
var gFinFormula = "=query(googlefinance(" + '"' + tickerToArr + '"' + ",'all shares'!A4,'all shares'!D3,'all shares'!D4,'all shares'!D5)," + '"' + "select *" + '"' + ",1)";
var repeated = [].concat(... new Array(105).fill(tickerToArr))
tickerArr.push(repeated)
}
}
Logger.log(tickerArr[0]);
TDSheet.getRange(TDSheet.getLastRow() + 1, 1, tickerArr.length, 1).setValues(tickerArr);
}
Appreciate any pointers!
From your following replying,
The array is composed of about 200k element, each showing in the array 105 times. I want to write all of them, one on the top of the other in a single column.
How about the following modification?
From:
tickerArr.push(repeated)
To:
tickerArr = tickerArr.concat(repeated);
References:
push()
concat()

How to create a datepicker in react js

I tried different method to develop date picker in reactJS. I done with "react-datepicker" package and it return "Wed May 15 2019 12:54:33 GMT+0100" as a result but I need 12/12/2000 format
try to format it as you want
const pickerDate = new Date('Wed May 15 2019 12:54:33 GMT+0100')
const day = pickerDate.getDay() < 10 ? '0' + pickerDate.getDay() : pickerDate.getDay()
const month = pickerDate.getMonth() + 1 < 10 ? '0' + (pickerDate.getMonth() + 1) : pickerDate.getMonth() + 1
const year = pickerDate.getFullYear()
const formatedDate = `${day}/${month}/${year}`
you can also use third part libraries like moment

Need to Format Current Date in Specific Format in React Native using Libraries

Actually in my app. I am trying to display current date & time in a specific format like this " 27/02/2019 1:40 PM ". I have done this by making use of custom formatting codes. But what i actually need is, I need to achieve this by making use of libraries.
Thanks for helping.!
Using this:(Manually formatting Date & Time)
var d = new Date();
var date = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
var hours = d.getHours();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12;
var min = d.getMinutes();
min = min < 10 ? '0'+min : min;
var result = date + '/' + month + '/' + year + ' ' + hours + ':' + min + ' ' + ampm;
console.log(result); //Prints DateTime in the above specified format
Use moment.js https://momentjs.com/
moment().format('DD/MM/YY h:mm A') will give you your desired output.

Reactjs DayPickerInput disabled days

I have a DayPickerInput element from react-day-picker plugin and I don't know how to disable all days after a month(31 days) starting with current day. Any help please?
Thanks.
The documentation could be a clearer. This should do it for you:
<DayPickerInput
value={moment(minDate).format('YYYY-MM-DD')}
dayPickerProps={{
disabledDays: {
after: new Date(2018, 3, 20),
},
}}
onDayChange={day => console.log(day)}
/>
Replace the new Date(y, m, d) with your date.
[Edit per my comment]
Not all months are 31 days, if you literally want to add 31 days to the first of a month:
Source: Add day(s) to a Date object
var myDate = new Date();
myDate.setDate(myDate.getDate() + AddDaysHere);
Where "AddDaysHere" would be 31.
If you just want to insure there is no way to select a date next month, you could:
// There is probably a billion better ways to get the next available month, this is just basic
let currentMonth = 2;
let nextMonth = currentMonth + 1;
if (nextMonth > 11) { nextMonth = 0;} // I believe javascript months start at 0.
Date(2018, nextMonth, 1)
Happy coding!

Manipulating character arrays quickly in R data.table [duplicate]

This question already has answers here:
Faster way to read fixed-width files
(4 answers)
Closed 4 years ago.
I have a huge datatset (14GB, 200 Mn rows) of character vector. I've fread it (took > 30 mins on 48 core 128 GB server). The string contains concatenated information on various fields. For instance, the first row of my table looks like:
2014120900000001091500bbbbcompany_name00032401
where the first 8 characters represent date in YYYYMMDD format, next 8 characters are id, next 6 the time in HHMMSS format and then next 16 are name (prefixed with b's) and the last 8 are price (2 decimal places).
I need to transfer the above 1 column data.table into 5 columns: date, id, time, name, price.
For the above character vector that will turn out to be: date = "2014-12-09", id = 1, time = "09:15:00", name = "company_name", price = 324.01
I am looking for a (very) fast and efficient dplyr / data.table solution. Right now I am doing it with using substr:
date = as.Date(substr(d, 1, 8), "%Y%m%d");
and it's taking forever to execute!
Update: With readr::read_fwf I am able to read the file in 5-10 mins. Apparently, the reading is faster than fread. Below is the code:
f = "file_name";
num_cols = 5;
col_widths = c(8,8,6,16,8);
col_classes = "ciccn";
col_names = c("date", "id", "time", "name", "price");
# takes 5-10 mins
data = readr::read_fwf(file = f, col_positions = readr::fwf_widths(col_widths, col_names), col_types = col_classes, progress = T);
setDT(data);
# object.size(data) / 2^30; # 17.5 GB
A possible solution:
library(data.table)
library(stringi)
widths <- c(8,8,6,16,8)
sp <- c(1, cumsum(widths[-length(widths)]) + 1)
ep <- cumsum(widths)
DT[, lapply(seq_along(sp), function(i) stri_sub(V1, sp[i], ep[i]))]
which gives:
V1 V2 V3 V4 V5
1: 20141209 00000001 091500 bbbbcompany_name 00032401
Including some additional processing to get the desired result:
DT[, lapply(seq_along(sp), function(i) stri_sub(V1, sp[i], ep[i]))
][, .(date = as.Date(V1, "%Y%m%d"),
id = as.integer(V2),
time = as.ITime(V3, "%H%M%S"),
name = sub("^(bbbb)","",V4),
price = as.numeric(V5)/100)]
which gives:
date id time name price
1: 2014-12-09 1 09:15:00 company_name 324.01
But you are actually reading a fixed width file. So could also consider read.fwf from base R or read_fwffrom readr or write your own fread.fwf-function like I did a while ago:
fread.fwf <- function(file, widths, enc = "UTF-8") {
sp <- c(1, cumsum(widths[-length(widths)]) + 1)
ep <- cumsum(widths)
fread(file = file, header = FALSE, sep = "\n", encoding = enc)[, lapply(seq_along(sp), function(i) stri_sub(V1, sp[i], ep[i]))]
}
Used data:
DT <- data.table(V1 = "2014120900000001091500bbbbcompany_name00032401")
Maybe your solution is not so bad.
I am using this data:
df <- data.table(text = rep("2014120900000001091500bbbbcompany_name00032401", 100000))
Your solution:
> system.time(df[, .(date = as.Date(substr(text, 1, 8), "%Y%m%d"),
+ id = as.integer(substr(text, 9, 16)),
+ time = substr(text, 17, 22),
+ name = substr(text, 23, 38),
+ price = as.numeric(substr(text, 39, 46))/100)])
user system elapsed
0.17 0.00 0.17
#Jaap solution:
> library(data.table)
> library(stringi)
>
> widths <- c(8,8,6,16,8)
> sp <- c(1, cumsum(widths[-length(widths)]) + 1)
> ep <- cumsum(widths)
>
> system.time(df[, lapply(seq_along(sp), function(i) stri_sub(text, sp[i], ep[i]))
+ ][, .(date = as.Date(V1, "%Y%m%d"),
+ id = as.integer(V2),
+ time = V3,
+ name = sub("^(bbbb)","",V4),
+ price = as.numeric(V5)/100)])
user system elapsed
0.20 0.00 0.21
An attempt with read.fwf:
> setClass("myDate")
> setAs("character","myDate", function(from) as.Date(from, format = "%Y%m%d"))
> setClass("myNumeric")
> setAs("character","myNumeric", function(from) as.numeric(from)/100)
>
> ff <- function(x) {
+ file <- textConnection(x)
+ read.fwf(file, c(8, 8, 6, 16, 8),
+ col.names = c("date", "id", "time", "name", "price"),
+ colClasses = c("myDate", "integer", "character", "character", "myNumeric"))
+ }
>
> system.time(df[, as.list(ff(text))])
user system elapsed
2.33 6.15 8.49
All outputs are the same.
Maybe try using matrix with numeric instead of data.frame. Aggregation should take less time.

Resources