I have the following apex code that displays the created date of records:
for(let i=0; i<emailMessages.length; i++){
replyBody += 'On '+ emailMessages[i].CreatedDate + ' ' + emailMessages[i].From_Name__c +' ' +'responded: ';
}
This above code displays the message like this:
On 2022-06-30T15:27:36.000Z Sarah Kat responded:
I want to change the date format so it can be displayed as below:
On Thu, Jun 30, 2022 at 4:26 PM Sarah Kat responded:
As there anyway where we can change the date format directly in apex?
Using these docs as my reference:
https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_datetime.htm#apex_System_Datetime_format
https://www.salesforcecodecrack.com/2020/04/format-dates-and-time-using-apex-in.html
The "format()" function will do what you want. The code would be:
Datetime createdDate = Datetime.now();
String converted = createdDate.format('E, MMM d, yyyy K:mm a');
System.debug(converted);
// Sat, Jul 2, 2022 5:21 AM
Just change out the Datetime.now() for the CreatedDate of the record you're using.
If you really want that specific format, you'll have to do some extra logic to put in the "at" piece. There are probably more elegant ways to do this but this was quick and easy for me to read.
Datetime createdDate = Datetime.now();
String converted = createdDate.format('E, MMM d, yyyy K:mm a');
String[] splitup = converted.split(' ');
String final_string = '';
for(Integer i = 0; i < splitup.size(); i++)
{
// right between year and hour
if(i == 4)
{
final_string = final_string + ' at';
}
final_string = final_string + ' ' + splitup[i];
}
System.debug(final_string);
// Sat, Jul 2, 2022 at 5:27 AM
UPDATE
Yeah, you needed to make the code a function and return the result into what you need. This will work.
for(let i=0; i<emailMessages.length; i++){
replyBody += 'On '+ datetime_formatting_email(emailMessages[i].CreatedDate) + ' ' + emailMessages[i].From_Name__c +' ' +'responded: ';
}
public String datetime_formatting_email(Datetime createdDate)
{
String converted = createdDate.format('E, MMM d, yyyy K:mm a');
String[] splitup = converted.split(' ');
String final_string = '';
for (Integer i = 0; i < splitup.size(); i++)
{
// right between year and hour
if (i == 4) {
final_string = final_string + ' at';
}
final_string = final_string + ' ' + splitup[i];
}
return final_string;
}
Related
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()
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.
I am fetching the date from my Django backend which comes like this: 2016-03-31
In my angular controller I want to compare it with today's date and if they are similar I want to deactivate a button.
I tried new Date() which gives me something like Thu Mar 31 2016 08:59:01 GMT+0200 (W. Europe Daylight Time)
How can these two dates be compared to achieve my goal?
ehhhhh... i think currently we could only do a manual formatting
see this one: How to format a JavaScript date
For your reference, below is what i did though:
$scope.formatDate = function(date){
var newDate = new Date(date);
var year = newDate.getFullYear();
var month = (newDate.getMonth() + 1).toString(); //add 1 as Jan is '0'
var day = newDate.getDate().toString();
month = month.length > 1? month: '0'+month;
day = day.length > 1? day: '0'+day;
$scope.date = day + '/' + month + '/' + year;
}
If you want to compare the date with now you can do the following:
var now = new Date().getTime();
var compareDate = new Date('2016-03-31').getTime();
if(now > compareDate){
//greater
}else if(now < compareDate){
//less
}else{
//equal
}
Just add what you need to the scope and then you could do something like:
ng-disabled="compareDate === today"
In the code below, I am trying to read in a 2d array (157 rows, 2 columns), which are all formulas. I need to then delete a part of the formula from each cell, then write it back to the sheet in the same range (prior to this step, I am deleting the sheet that this portion of the formula is referencing). When I run the debugger, everything runs fine, but my values do not get replaced. Any thoughts?
ss.setActiveSheet(ss.getSheetByName('Employee List'));
var as = SpreadsheetApp.getActiveSheet();
var empid = as.getLastRow();
var hrsRange = as.getRange(1, 3, empid, 2).getFormulas();
var i = 1;
while(i < empid)
{
var emp = as.getRange(i, 2).getA1Notation();
if (cpr == 20)
{
while(i < hrsRange.length)
{
var regHrs = "+sumif('" + array[x] + "'!O:O,$" + emp + ",'" + array[x] + "'!P:P)";
var otHrs = "+sumif('" + array[x] + "'!O:O,$" + emp + ",'" + array[x] + "'!Q:Q)";
hrsRange[i-1][0].toString().replace(regHrs,"");
hrsRange[i-1][1].toString().replace(otHrs,"");
i++;
}
} else {
while(i < hrsRange.length)
{
var regHrs = "+sumif('" + array[x] + "'!O:O,$" + emp + ",'" + array[x] + "'!Q:Q)";
var otHrs = "+sumif('" + array[x] + "'!O:O,$" + emp + ",'" + array[x] + "'!R:R)";
hrsRange[i-1][0].toString().replace(regHrs,"");
hrsRange[i-1][1].toString().replace(otHrs,"");
i++;
}
}
i++;
}
as.getRange(1, 3, empid, 2).setFormulas(hrsRange);
This line:
hrsRange[i-1][0].toString().replace(regHrs,"");
will return a string, not actually modify hrsRange[i-1][0] .
You'll need to assign the resulting string to hrsRange[i-1][0]:
hrsRange[i-1][0]=hrsRange[i-1][0].toString().replace(regHrs,"");
I am trying to schedule a class to run every 15 minutes. I know we can set in salesforce for every hour, but is there a way to reduce the granularity to 10-15 minutes?
global class scheduledMerge implements Schedulable {
global void execute(SchedulableContext SC) {
ProcessTransactionLog p= new ProcessTransactionLog();
p.ProcessTransaction();
}
}
You can use this apex code snippet to schedule your job to run every 15 minutes.
System.schedule('Job1', '0 0 * * * ?', new scheduledMerge());
System.schedule('Job2', '0 15 * * * ?', new scheduledMerge());
System.schedule('Job3', '0 30 * * * ?', new scheduledMerge());
System.schedule('Job4', '0 45 * * * ?', new scheduledMerge());
global class scheduledTest implements Schedulable {
global void execute(SchedulableContext SC) {
RecurringScheduleJob.startJob();
String day = string.valueOf(system.now().day());
String month = string.valueOf(system.now().month());
String hour = string.valueOf(system.now().hour());
String minute = string.valueOf(system.now().minute() + 15);
String second = string.valueOf(system.now().second());
String year = string.valueOf(system.now().year());
String strJobName = 'Job-' + second + '_' + minute + '_' + hour + '_' + day + '_' + month + '_' + year;
String strSchedule = '0 ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ?' + ' ' + year;
System.schedule(strJobName, strSchedule, new scheduledTest());
}
}