If I have a date YYMMDDHHmmss such as 190525234530 how do I work out the smallest number of characters to represent this using 0-9a-z (36 characters)?
I believe there are 3,153,600,000 combinations (100 years * 365 days * 24 hours * 60 minutes * 60 seconds) which would fit into 32 bits. Does this mean I could represent these dates using 4 characters?
I am a bit lost as to how to do the conversion so if anyone could show me the maths that would be greatly appreciated.
I ended up doing this in JavaScript, I decided I wanted to compress to 6 characters so I created my own time which generates unique ID's for up to 68 years from 01/01/2019 which worked for me.
function getId() {
//var newTime = parseInt(moment().format("X")) - 1546300800;//seconds since 01/01/2019
var newTime = Math.floor((new Date()).getTime() / 1000) - 1546300800;//seconds since 01/01/2019
var char = "0123456789abcdefghijklmnopqrstuvwxyz";//base 36
return char[Math.floor(newTime / 36**5)]+
char[Math.floor(newTime%36**5 / 36**4)]+
char[Math.floor(newTime%36**4 / 36**3)]+
char[Math.floor(newTime%36**3 / 36**2)]+
char[Math.floor(newTime%36**2 / 36)]+
char[Math.floor(newTime%36)];
}
console.log(getId());
Thanks to #user956584 this can be be changed to:
function getId() {
//var newTime = parseInt(moment().format("X")) - 1546300800;//seconds since 01/01/2019
var newTime = Math.floor((new Date()).getTime() / 1000) - 1546300800;//seconds since 01/01/2019
return newTime.toString(36);
}
console.log(getId());
Related
I have a one time column in format 00:01:30 (HH:MM:SS). This column is a text format column.
this text column format convert to a time format column and create a new measure for the total time sum.
You can easily convert your Text column into Time using
Time = TIMEVALUE('Table'[Text])
But the problem is that the Time format doesn't support more than 24 hours, so your SUM will potentially lead to an overflow. Here's a workaround:
Create a calculated "Seconds" Column
Seconds =
VAR Time =
TIMEVALUE('Table'[Text])
RETURN
HOUR(Time) * 3600 + MINUTE(Time) * 60 + SECOND(Time)
Aggregate the Seconds with this Measure and convert back to a "Duration-like" format:
Total Duration =
VAR total_seconds =
SUM('Table'[Seconds])
VAR days =
QUOTIENT(total_seconds, 24 * 60 *60)
VAR rest1 =
MOD(total_seconds, 24 * 60 * 60)
VAR hours =
QUOTIENT(rest1, 60 * 60)
VAR rest2 =
MOD(total_seconds, 60 * 60)
VAR minutes =
QUOTIENT(rest2, 60)
VAR seconds =
MOD(rest2, 60)
RETURN
days & "." & FORMAT(hours, "0#") & ":" & FORMAT(minutes, "0#") & ":" & FORMAT(seconds, "0#")
I would like to find out the number of hours and minutes between two date time stamp.
if for example
sDateTime = 2016-01-01 01:00
eDateTime = 2016-01-03 02:30
I would like it to output it as 49:30 (49hours and 30minutes)
I am unable to figure a method to work this out.
what I have so far:
Set oMNOF=##class(MNOF.MNOF).%OpenId(Id)
Set zstartDt=oMNOF.sDateTime
Set startDt=$PIECE(zstartDt,",",1)
Set startTime=$PIECE(zstartDt,",",2)
Set zendDt=oMNOF.eDateTime
Set endDt=$PIECE(zendDt,",",1)
Set endTime=$PIECE(zendDt,",",2)
set dateDiff=((endDt - startDt)) //2 days
set timeDiff=(endTime - startTime) //outputs 5400 seconds
set d = (dateDiff * 24 * 60 * 60)
set h = ((timeDiff - d) / 60)
set m = timeDiff - (d) - (h * 60)
Thank you for the help.
Another option:
USER>set mm=$system.SQL.DATEDIFF("mi","2016-01-02 01:00","2016-01-03 02:30")
USER>write "hours=", mm \ 60
hours=25
USER>write "minutes=", mm # 60
minutes=30
Hi thanks to all for the help.
I managed to come up with the below, appreciate if someone can improve on this.
<script language="cache" method="MGetData" arguments="pStartDt:%String,pEndDt:%String,pTimeField:%String" returntype="%Library.String">
set val1="00"
//HOUR: check if length equals 1
if $LENGTH($SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/3600))=1{
//add leading zero
set val1 ="0"_$SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/3600)
}
else{
//get without leading zero
set val1 = $SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/3600)
}
//MINUTES: check if length equals 1
if $LENGTH($SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/60) - ($SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/3600)*60))=1{
//add leading zero
set val2 ="0"_($SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/60) - ($SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/3600)*60))
}
else{
//get without leading zero
set val2 = ($SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/60) - ($SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/3600)*60))
}
//insert result data into the time field
Write "document.getElementById('"_pTimeField_"').value='"_val1_":"_val2_"';"
//Write "alert('"_val1_"^"_val2_"');"
QUIT 1
This is very weird. Ok, the below code works fine
public void setCookie(String cookiesName, String cookiesValue){
final int COOKIE_TIMEOUT = 1000 * 60 * 60 * 24;//1 days
Date expires = new Date((new Date()).getTime() + COOKIE_TIMEOUT);
Cookies.setCookie(cookiesName, cookiesValue, expires);
}
//then
setCookie("currentLang","de");
Collection<String> cookies = Cookies.getCookieNames();
for(String cookie : cookies){
if("currentLang".equals(cookie)){
System.out.println("got currentlang");
}
}
If i run the above code then I can see output: "got currentlang"
However, if I set timeout=30 days final int COOKIE_TIMEOUT = 1000 * 60 * 60 * 24 * 30;//30 days, then nothing got printed out, so "currentLang" has not even been stored if we set 30 days?
Why is that? does Gwt prevent that to happen?
You're using integer arithmetic for a sum that overflows beyond the maximum positive value for an integer. So 1000*60*60*24*30 is negative.
You could try 1000L*60*60*24*30
I have a float value, what i need is convert my float to date. For example i have float 50,55, i want to put on a label value like this: 50 years 6 month 6 hours 6 mins 30,04 sec.
And i wonder how to make this label change every 0.01 second for change value. Please provide me solution to convert my float value to date format, any help would be appreciated.
This is code i use for create my float:
-(float)calculationResult{
lifeTime = (kCountry +kEdu - kBirth - kSmo -kAlco+kDis+kHap+kDri+kMar)*kWei*kEnv*kSle;
return lifeTime;
}
The following code splits the given number of "fractional years" into
years, month, days and hours. It makes the simplifying assumptions that every year
has 365 days, and that one month is exactly 1/12 of a year.
float tmp = lifeTime;
int years = tmp;
tmp = (tmp - years) * 12.;
int months = tmp;
tmp = (tmp - months) * 365./12.;
int days = tmp;
tmp = (tmp - days) * 24.;
int hours = tmp;
NSString *str = [NSString stringWithFormat:#"%d years %d month %d days %d hours ",
years, months, days, hours];
To update a label regularly, you can use the NSTimer class (but updating every 1/100 second is much too often).
I need to be able to compare the number of whole days between two dates in ActionScript, is this possible?
I'd like to test if one date is 7 days or less after today, and if so is it one day or less (if it's before today this also counts).
The workaround I have in place is using the .time part of the date field:
// Get the diffence between the current date and the due date
var dateDiff:Date = new Date();
dateDiff.setTime (dueDate.time - currentDate.time);
if (dateDiff.time < ( 1 * 24 * 60 * 60 * 1000 ))
return "Date is within 1 day");
else if (dateDiff.time < ( 7 * 24 * 60 * 60 * 1000 ))
return "Date is within 7 days");
As I say - this is only a workaround, I'd like a permanent solution to allow me to check the number of whole days between 2 dates. Is this possible?
Thanks
var daysDifference:Number = Math.floor((dueDate.time-currentDate.time)/(1000*60*60*24));
if (daysDifference < 2)
return "Date is within 1 day";
else if (daysDifference < 8)
return "Date is within 7 days";