Get Years months days hours minutes from moment from duration - react-native

const seconds = 1000000
var now = moment(moment.duration(seconds).asSeconds()).format('Y[y],M[m],D[d],H[h]')
I want to format this seconds to years, months, days,hours,minutes so I am not able to get where I am making mistake how can I get this format in React native

this code might help you
export function formatDate(seconds) {
var d = new Date(seconds*1000),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [day,month,year].join('/');
}
this code will return you dd/mm/yyyy. if you want to return date+time then you simply return
export function formatDate(seconds) {
var d = new Date(seconds*1000)
return d.toLocaleString()
}
if you want to get remains time, day, month and year then you should use this code
function secondsToHms(sec) {
sec = Number(sec);
var y = Math.floor(sec/31536000) //<<years
var w= Math.floor(y*31536000)
var w1=Math.floor(w-sec)
w=Math.floor(Math.abs(w1)/604800)//<<weeks
var d=Math.floor(Math.abs(w)*604800)
var d1=Math.floor(Math.abs(d)-Math.abs(w1))
d=Math.floor(Math.abs(d1)/86400)
d=Math.abs(d) //<<day
var h = Math.floor(d*86400);
h=Math.floor(h-Math.abs(d1))
h=Math.floor(Math.abs(h)/3600)//<<hours
var m = Math.floor(sec % 3600 / 60);//<<minutes
var s = Math.floor(sec % 3600 % 60);//<<seconds
var yDisplay = y > 0 ? y + (y == 1 ? " year, " : " years, ") : "0 year, ";
var wDisplay = w > 0 ? w + (w == 1 ? " week, " : " weeks, ") : "0 week, ";
var dDisplay = d > 0 ? d + (d == 1 ? " day, " : " days, ") : "0 day, ";
var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : "0 hour, ";
var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : "0 minute, ";
var sDisplay = s > 0 ? s + (s == 1 ? " second. " : " seconds. ") : "0 second. ";
return yDisplay + wDisplay + dDisplay + hDisplay + mDisplay + sDisplay;
}
console.log(secondsToHms(1000000))

Related

How to store the first N rows of the current record as an array vector?

I defined a table as follows:
syms = "A"
datetimes = 2021.01.01..2022.01.01
n = 200
t = table(take(datetimes,n) as trade_time, take(syms,n) as sym,take(500+rand(10.0,n), n) as price)
You can try the following script:
syms = "A"
datetimes = 2021.01.01..2022.01.01
n = 200
t = table(take(datetimes,n) as trade_time, take(syms,n) as sym,take(500+rand(10.0,n), n) as price)
dayNum = 3
tbName = "t"
def getLastNDay(tbName, dayNum){
colName = "price"
scripts = "update " + tbName + " set lastNPrice = fixedLengthArrayVector("
for(n in 1..(dayNum-1)){
scripts = scripts + "move(" + colName + "," + n + "),"
}
scripts = scripts +"move(" + colName + "," + dayNum + "))"
print(scripts)
runScript(scripts)
}
getLastNDay(tbName, dayNum)
Output:

Import data into sql developer from .csv file

I have to import some data in my Shipment table from .csv file,but i have problem with imorting DATE.
Here is you can see my generator:
class Shipment extends Generator
{
private String snd_time;
private String dliv_time;
private int deliv_id;
private int post_id;
int d, m, y, rnd;
public Shipment() throws FileNotFoundException, Exception
{
PrintWriter wr = new PrintWriter("ShipmentData.csv");
for(int i = 1; i < 100001; i++){
m = 1 + r.nextInt(12);
y = 2005 + r.nextInt(16);
if(m == 1 || m == 3 || m ==5 || m ==7 || m ==8 || m ==10 || m ==12)
d = 1+r.nextInt(31);
if(m == 4 || m == 6 || m == 9 || m == 11)
d = 1 + r.nextInt(30);
if((y % 4 == 0) && m == 2)
d = 1 + r.nextInt(29);
if((y % 4 ) != 0 && m == 2 )
d = 1 + r.nextInt(28);
if(m >= 10 && d >= 10)
snd_time = Integer.toString(d) + "/" + Integer.toString(m) + "/" + Integer.toString(y);
if(m < 10 && d < 10)
snd_time = "0" + Integer.toString(d) + "/" + "0" + Integer.toString(m) + "/" + Integer.toString(y);
if(d < 10 && m >= 10)
snd_time = "0" + Integer.toString(d) + "/" + Integer.toString(m) + "/" +Integer.toString(y);
if(m < 10 && d >= 10)
snd_time = Integer.toString(d) + "/" + "0" + Integer.toString(m) + "/" + Integer.toString(y);
rnd = 1 + r.nextInt(7);
if(m == 1 || m == 3 || m ==5 || m ==7 || m ==8 || m ==10 || m ==12)
{
if((d + rnd) > 31 ) {
d = (d + rnd) - 31;
m++;
if(m > 12) {
m = 1;
y++;
}
}
if((d + rnd) < 31)
d += rnd;
}
if(m == 4 || m == 6 || m == 9 || m == 11)
{
if((d + rnd) > 30) {
d = (d + rnd) - 30;
m++;
if(m > 12) {
m = 1;
y++;
}
}
if((d + rnd) < 30)
d += rnd;
}
if((y % 4 == 0) && m == 2)
{
if((d + rnd) > 29) {
d = (d + rnd) - 29;
m++;
if(m > 12) {
m = 1;
y++;
}
}
if((d + rnd) < 29)
d += rnd;
}
if((y % 4 ) != 0 && m == 2 )
{
if((d + rnd) > 28) {
d = (d + rnd) - 28;
m++;
if(m > 12) {
m = 1;
y++;
}
}
if((d + rnd) < 28)
d += rnd;
}
if(m >= 10 && d >= 10)
dliv_time = Integer.toString(d) + "/" + Integer.toString(m) + "/" + Integer.toString(y);
if(m < 10 && d < 10)
dliv_time = "0" + Integer.toString(d) + "/" /*+ "0"*/ + Integer.toString(m) + "/" + Integer.toString(y);
if(d < 10 && m >= 10)
dliv_time = "0" + Integer.toString(d) + "/" + Integer.toString(m) + "/" +Integer.toString(y);
if(m < 10 && d >= 10)
dliv_time = Integer.toString(d) + "/" + "0" + Integer.toString(m) + "/" + Integer.toString(y);
deliv_id = 1 + r.nextInt(100000);
post_id = 1 + r.nextInt(100000);
wr.println(i + "," + this.snd_time + "," + this.dliv_time + "," + this.deliv_id + "," + this.post_id );
}
wr.close();
}
}
Here is my ctl file:
LOAD DATA
INFILE 'D:\databaseProject\Data\ShipmentData.csv'
INSERT INTO TABLE shipment
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
id,
send_time,
deliv_time DATE "dd/mm/yyyy",
delivery_id DATE "dd/mm/yyyy",
post_id
)
but when i load this ctl file i get this error
Record 4: Rejected - Error on table SHIPMENT, column SEND_TIME.
ORA-01843: not a valid month
I've tried to write my date without 0, but it still doesn't work and i cannot find a solution for my problem

Unable to Retrieve Reminder Details from Device Calendar

I am able to retrieve all the events from the device calendar from the below code,
#SuppressLint("MissingPermission")
private fun getAccountCalendarEvents(selectedEmail: String) {
var count = 0
val projectionArray =
arrayOf(
CalendarContract.Calendars._ID,
CalendarContract.Events.TITLE,
CalendarContract.Events.DESCRIPTION,
CalendarContract.Events.DTSTART,
CalendarContract.Events.DTEND,
CalendarContract.Events.ALL_DAY,
CalendarContract.Events.EVENT_LOCATION,
CalendarContract.Attendees.ORGANIZER,
CalendarContract.Events.EVENT_TIMEZONE,
CalendarContract.Events.ACCOUNT_TYPE,
CalendarContract.Calendars.ACCOUNT_NAME
)
val startTime = Calendar.getInstance()
startTime.set(Calendar.HOUR_OF_DAY, 0)
startTime.set(Calendar.MINUTE, 0)
startTime.set(Calendar.SECOND, 0)
startTime.add(Calendar.YEAR, -2)
val endTime = Calendar.getInstance()
endTime.add(Calendar.YEAR, 2)
val selection =
"(( " + CalendarContract.Events.DTSTART + " >= " + startTime.timeInMillis + " ) AND ( " + CalendarContract.Events.DTEND + " <= " + endTime.timeInMillis + " ) AND ( deleted != 1 ) AND (account_name = '" + selectedEmail + "'))"
val cursor = contentResolver.query(
CalendarContract.Events.CONTENT_URI,
projectionArray,
selection,
null,
null
)
if (cursor != null && cursor.count > 0 && cursor.moveToFirst()) {
do {
var calendarString = ""
val calendarId = "Id: " + cursor.getInt(0)
val title = "Title: " + cursor.getString(1)
val description = "Description: " + cursor.getString(2)
val startDate = "Start Date: " + cursor.getString(3)
val endDate = "End Date: " + cursor.getString(4)
val allDay = "All Day: " + cursor.getString(5)
val location = "Location: " + cursor.getString(6)
val organizer = "Organizer: " + cursor.getString(7)
val timeZone = "Timezone: " + cursor.getString(8)
val accountType = "Account Type: " + cursor.getString(9)
val accountName = "Account Name: " + cursor.getString(10)
calendarString =
calendarId + "\n" + title + "\n" + description + "\n" + startDate + "\n" + endDate + "\n" + allDay + "\n" + location + "\n" + organizer + "\n" + timeZone + "\n" + accountType + "\n" + accountName
result = if (result.isEmpty()) {
calendarString
} else {
result + "\n\n\n" + calendarString
}
count++
} while (cursor.moveToNext())
}
// txtResults.text = result
Log.e("DCal", "" + count)
getEventsReminders(selectedEmail)
}
This works absolutely fine and perfect.Whereas, when I'm trying to fetch the Reminders from the device with the following code,
#SuppressLint("MissingPermission")
private fun getAccountCalendarReminders(selectedEmail: String) {
var count = 0
val projectionArray =
arrayOf(
CalendarContract.Reminders.EVENT_ID,
CalendarContract.Reminders.METHOD,
CalendarContract.Reminders.MINUTES
)
val startTime = Calendar.getInstance()
startTime.add(Calendar.YEAR, -2)
val endTime = Calendar.getInstance()
endTime.add(Calendar.YEAR, 2)
// val selection =
// "((account_name = '$selectedEmail'))"
val cursor = contentResolver.query(
CalendarContract.Reminders.CONTENT_URI,
projectionArray,
null,
null,
null
)
if (cursor != null && cursor.count > 0 && cursor.moveToFirst()) {
do {
var calendarString = ""
val calendarId = "Id: " + cursor.getInt(0)
val title = "Method: " + cursor.getString(1)
val description = "Minutes: " + cursor.getString(2)
// val startDate = "Title: " + cursor.getString(3)
// val endDate = "End Date: " + cursor.getString(4)
// val allDay = "All Day: " + cursor.getString(5)
// val location = "Location: " + cursor.getString(6)
// val organizer = "Organizer: " + cursor.getString(7)
// val timeZone = "Timezone: " + cursor.getString(8)
// val accountType = "Account Type: " + cursor.getString(9)
// val accountName = "Account Name: " + cursor.getString(10)
calendarString =
calendarId + "\n" + title + "\n" + description
result = if (result.isEmpty()) {
calendarString
} else {
result + "\n\n\n" + calendarString
}
count++
} while (cursor.moveToNext())
}
Log.e("DRem", "" + count)
txtResults.text = result
}
And the output of this Reminders are just,
Could anyone please help me in getting the Reminders Information completely like,
1. Reminder Name,
2. Start Date,
3. Location, etc. the same fields which Events do have.
Note:
I have gone through this library as well,
https://github.com/EverythingMe/easy-content-providers
This library does the same. It doesn't list the reminders from the device calendar.
Code Snippet from the above Lib:
val provider = CalendarProvider(this)
val events = provider.getEvents(id.toLong()).list as ArrayList<Event>
val reminders = provider.getReminders(id.toLong()).list as ArrayList<Event>

SQL Join Query taking long to complete

I am working on a project that require me to join four tables. I have written this code but it's taking forever to finish. Please help. Ohhh I have about 121 000 entries in the Db
PortfolioCollectionDataContext context = null;
context = DataContext;
var Logins = from bkg in context.EnquiryBookings
where bkg.Paid == true
from log in context.Logins
where log.LoginID == bkg.LoginID
from enq in context.Enquiries
where enq.EnquiryID == bkg.EnquiryID
from estb in context.Establishments
where enq.EstablishmentID == estb.EstablishmentID
select new
{
log.LoginID,
log.FirstName,
log.LastName,
log.CountryOfResidence,
log.EmailAddress,
log.TelephoneNumber,
bkg.TotalPrice,
estb.CompanyName
};
string str = "";
foreach (var user in Logins)
{
str += ("[Name: " + user.LastName + " " + user.FirstName + " - Country: " + user.CountryOfResidence + " - Phone: " + user.TelephoneNumber + " - Email: " + user.EmailAddress + " - Booked From: " + user.CompanyName + " - Spent: " + user.TotalPrice.ToString() + "]");
}
return str;
Use following query on LINQ
PortfolioCollectionDataContext context = null;
context = DataContext;
var Logins = from bkg in context.EnquiryBookings
join log in context.Logins
on log.LoginID equals bkg.LoginID
&& bkg.Paid == true
join enq in context.Enquiries
on enq.EnquiryID equals bkg.EnquiryID
join estb in context.Establishments
on enq.EstablishmentID == estb.EstablishmentID
select new
{
str = "[Name: " + log.LastName + " " + log.FirstName + " - Country: " + log.CountryOfResidence + " - Phone: "
+ log.TelephoneNumber + " - Email: " + log.EmailAddress + " - Booked From: "
+ estb.CompanyName + " - Spent: " + bkg.TotalPrice.ToString() + "]"
};
string output = string.Join(", ", Logins.ToList());
return output;
Check your query by taking cursor on Logins and paste that query here. Then check an estimated execution plan of your query and paste here.
If using SQL Server, to get execution plan of your query using Sql server management studio, click on an icon highlighted in an image below.

compute age from given birthdate

I have 2 comboboxes and 2 textboxes. My first combobox contains months in this format january, february, etc, and the other combobox contains numbers from 1 to 31. My first textbox is txtyear. Once the user input birth year to txtyear a variable BOD will be equals to this.
Dim BOD as string
BOD = cbomonth.text + "-" + cboday.text + "-" + txtyear.text
The purpose of my last textbox is to handle the age of the user that will be computed when the cursor lost focus on txtyear.
Can anyone help how to compute the age.
There are really two questions here:
How to convert the string input into a DateTime object
How to calculate age once you have your data in the correct format.
I'll let you follow other's instructions for how use TryParseExtract which is definitely the correct way to go here.
When determining someone's age from their DOB, try using this:
Public Function GetCurrentAge(ByVal dob As Date) As Integer
Dim age As Integer
age = Today.Year - dob.Year
If (dob > Today.AddYears(-age)) Then age -= 1
Return age
End Function
It is the vb version of the top answers on Jeff Atwood's very popular question How do I calculate someone's age
I wrote a blogpost about calculating age from dob as well.
Here's a little different way using the year and month properties of the Date class:
Dim BOD as string
BOD = cbomonth.text + "-" + cboday.text + "-" + txtyear.text
Dim dt As Date
If Date.TryParseExact(BOD, "MMMM-dd-yyyy", Nothing, Globalization.DateTimeStyles.None, dt) Then
Dim Age As New Date(Now.Subtract(dt).Ticks)
MsgBox(String.Format("Your age is : {0} Years and {1} Months", Age.Year - 1, Age.Month - 1))
Else
MsgBox("Birth Date is in wrong format")
End If
Here's a technique when you use Visual Studio 2012
VB.NET language
Private Sub dtpBOfD_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpBOfD.ValueChanged
lblAge.Text = Age(dtpBOfD.Value)
End Sub
Public Shared Function Age(DOfB As Object) As String
If (Month(Date.Today) * 100) + Date.Today.Day >= (Month(DOfB) * 100) + DOfB.Day Then
Return DateDiff(DateInterval.Year, DOfB, Date.Today)
Else
Return DateDiff(DateInterval.Year, DOfB, Date.Today) - 1
End If
End Function
Use this function
Function String2Date(ByVal sDay As String, ByVal sMonth as String, ByVal sYear as String) As Date
StdDateString = sMonth & " " & sDay & ", " & sYear
End Function
And apply it ..
Dim dt1 as Date = String2Date(ComboBox2.Text,ComboBox1.Text,txtYear.Text).ToShortDateString
Dim dt2 as Date = Now.ToShortDateString
Dim dt3 as TimeSpan = (dt2 - dt1)
Dim diff as Double = dt3.Days
Dim sAge as String
sAge = Str(Int(diff / 365)) & " Year "
diff = diff Mod 365
sAge = sAge & Str(Int(diff / 30)) & " Month(s)"
diff = diff Mod 30
sAge = sAge & Str(diff) & " Day(s)"
txtAge.Text = sAge
for complete age information use this code in c#.`
public string calculateDays(int day, int Month, int year)
{
int Diffyear;
int DiffMonth;
int DiffDay;
int cuYear=DateTime.Now.Year;
int cuMonth=DateTime.Now.Month;
int cuDay=DateTime.Now.Day;
string Age;
Diffyear= cuYear-year;
DiffMonth=cuMonth-Month;
DiffDay=cuDay-day;
if ((DiffMonth) < 0)
{
Diffyear -= 1;
}
if ((DiffDay) < 0)
{
DiffMonth -= 1;
if ((cuMonth - 1) < 8)
{
if (((cuMonth - 1) % 2) == 0)
{
if ((cuMonth - 1) == 2)
if (cuYear % 4 == 0)
{
DiffDay = 29 + DiffDay;
}
else
{
DiffDay = 28 + DiffDay;
}
else
DiffDay = 30 + DiffDay;
}
else
DiffDay = 31 + DiffDay;
}
else
{
if (((cuMonth - 1) % 2) == 0)
{
DiffDay = 31 + DiffDay;
}
else
{
DiffDay = 30 + DiffDay;
}
}
}
if ((DiffMonth) < 0)
{
DiffMonth = DiffMonth+12;
}
if (Diffyear < 0)
{
Diffyear = Diffyear * (-1);
}
if ((DiffDay) < 0)
{
DiffDay = DiffDay * (-1);
}
Age = "Age: " + Diffyear.ToString() + " year, " + DiffMonth.ToString() + " months, " + DiffDay.ToString() + " days";
return Age;
}
`
Dim d1 As Date
Dim d2 As Date
d1 = Format(dob.Value, "yyyy/MM/dd"
d2 = Format(System.DateTime.Now, "yyyy/MM/dd")
d = DateDiff(DateInterval.Year, d1, d2)'d-1 provides accurate age