convert my string to AM or PM hour in ActionScript 3 - string

I've got a String variable for my time :
var $myTime:String;
//Some calculation
trace(myTime);
// Result is : 14:25 (for example)
I'm looking for a way to convert this string ("14:25" in this example) to a simple AM / PM format.
So in this example it would be, for example, 2 PM
Any idea how I can simply do that ?

You can use DateFormatter class to set your wished pattern.
You can write your code as follow:
var df:DateFormatter = new DateFormatter();
df.formatString = "YYYY-MM-DD L:NN:SS";
df.format(myTime);
Where L is HOUR with PM/AM; NN are the minutes SS seconds.
You can see the complete guide, about DateFormatter pattern, here

var myTime:Date = new Date();
var timeString:String;
if (myTime.getHours() > 12)
{
timeString = String((myTime.getHours() - 12) + ":" + myTime.getMinutes() + " PM");
}
if (myTime.getHours() == 12)
{
timeString = String(myTime.getHours() + ":" + myTime.getMinutes() + " PM");
}
if (myTime.getHours() < 12)
{
timeString = String(myTime.getHours() + ":" + myTime.getMinutes() + " AM");
}
trace(timeString);
That should do the trick.

Related

math.ceil groovy script return value

I am trying to generate a random date between two dates, could you check the below code?
What i notice is the output as : 02.0/01.0/1918.0
How can i save it as 02/02/1918 instead of 02.0/01.0/1918.0
var dob;
//set a range of years
var min = 1900;
var max = 2004;
// Math.ceil prevents the value from being 0;
var month = Math.ceil(Math.random() * 12);
var day = Math.ceil(Math.random() * 28);
var year = Math.floor(Math.random() * (max - min) + min);
//this ensures that the format will stay mm/dd/yyyy;
if(month < 10) {
month = "0" + month;
}
if(day < 10) {
day = "0" + day;
}
//concatenates random dob in mm/dd/yyyy format;
dob = month + "/" + day + "/" + year;
return dob;
Math.floor returns double. You have to convert it to int.
var year = Math.floor(...) as int

I am trying to get the time difference between the execution of 2 commands, one is !on and the other !off (Discord.JS))

I have the following code. It works to an extent, but when I do the !off command, it shows me a 0 instead of the actual time difference.
Index Code
global.time = getDateTime()
global.oldTime = getNewDateTime().getTime
global.finalTime = parseFloat(time) - parseFloat(oldTime)
function getDateTime() {
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
return hour + ":" + min;
}``
console.log(parseFloat(getDateTime))
function getNewDateTime() {
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
return hour + ":" + min;
}
And here is the off command, same with on almost
exports.run = async (client, message, args, tools) => {
const embed = new Discord.MessageEmbed()
.setColor("BLUE")
.addField("Timer ended. Total Time Online", `**: ${finalTime}**`)
message.channel.send(embed);
}

Color console.log timestamps?

So, I'm using this code from 6 years ago and would like to color the timestamps however I don't know where to put the colors.
var log = console.log;
console.log = function () {
var first_parameter = arguments[0];
var other_parameters = Array.prototype.slice.call(arguments, 1);
function formatConsoleDate (date) {
var hour = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
return '[' +
((hour < 10) ? '0' + hour: hour) +
':' +
((minutes < 10) ? '0' + minutes: minutes) +
':' +
((seconds < 10) ? '0' + seconds: seconds) +
'] ';
}
log.apply(console, [formatConsoleDate(new Date()) + first_parameter].concat(other_parameters));
};
Any help would be appreciated.
Edit: I was able to color the timestamps without any modules or anything by putting a color at the END of every console.log, and it colors the next line. I would assume there is a better way to do this however.
console.log('\x1b[36m%s\x1b[0m', 'colored word',' \x1b[32m\x1b[0');That for example, would color the consoles timestamps green.
Check the article on the Node.js knowledgebase: How to get colors on the command line

Trying to calculate the end of the next day in node.js

I'm trying to calculate the end of the next day for any given date as an expiry date.
So if the input is eg 10/26/2013 16:36:46 then I'd like to get 10/27/2013 23:59:59 and 7/31/2013 16:36:46 would give me 8/1/2013 23:59:59
It gets obviously difficult with leap years and when the next day is a new month. So I tried to use the dateParse function which gives me a number that I can easily add 86400000 (full day in milliseconds) and then I want to use basically the reverse of dateParse (turn a number into a date object) so that I can then take out the date component and replace the time component with 23:59:59
Here's my code which doesn't work - setTime doesn't seem to be the right function and makes my next_day variable "undefined"
var next_day = call_start.dateParse + 86400000; // adding a full day to it
next_day = next_day.setTime;
var day = next_day.getDate;
day = (day < 10 ? "0" : "") + day;
var month = next_day.getMonth + 1;
month = (month < 10 ? "0" : "") + month;
var year = next_day.getFullYear;
var hour = 23;
var min = 59;
var sec = 59;
var expiry = year + ":" + month + ":" + day + ":" + hour + ":" + min + ":" + sec;
console.log ('Streak expires at: ' + expiry);
You can use the set* functions to modify the date:
var date = new Date('7/31/2013 16:36:46');
date.setDate(date.getDate() + 1);
date.setHours(23);
date.setMinutes(59);
date.setSeconds(59);
console.log(date);
// Thu Aug 01 2013 23:59:59 GMT+0200 (CEST)

Comparing #Now to a Date/Time field?

How do I compare #Now to a data / time value in a document? This is what I have
var ProjectActiveTo:NotesDateTime = doc.getItemValueDateTimeArray("ProjectActiveTo")[0];
var ProjectExpired;
var d1:Date = #Now();
if (d1 > ProjectActiveTo.toJavaDate())
{
dBar.info("Today: " + d1 + " > " + ProjectActiveTo.toJavaDate());
ProjectExpired = true;
}
else
{
dBar.info("Today: " + d1 + " < " + ProjectActiveTo.toJavaDate());
ProjectExpired = false;
}
But this always seems to return false. I printed out some diagnostic messages.
Today: 1/18/13 6:02 PM < 1/20/01 5:00 PM
Obviously today is greater than 1/20/01 but this is the result of my test. Why?
I have done some searching and saw that the compare member function might be used but it returns an error for me and compare is not in the intelisense (or whatever Lotus calls it) in the design editor.
Found this little snippet on line - it should point you in the right direction
var doc:NotesDocument = varRowView.getDocument();
var d:NotesDateTime = doc.getItemValue("BidDueDate")[0];
var t:NotesDateTime = session.createDateTime("Today");
if (d.timeDifference(t) > 0 ) {
return "Overdue";
}else{
return "";
}

Resources