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

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.

Related

Salesforce change the format of created data in apex class

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;
}

How to format difference between 2 moment date

There is start date and end date. Using moment ,will get difference between 2 dates in hours.
var now = moment(sessionData.StartTime);
var end = moment(sessionData.EndTime);
var duration = moment.duration(end.diff(now));
var days = duration.asHours();
it returns : 3.08 .
I want to show that difference like this- 3 hrs 15 m.
Is this possible to achieve
Try get the difference between two days using diff followed by expressing it terms of duration. Then you would be able to get exact number of days, hours, minutes
let now = moment("2017-01-26T14:21:22+0000");
let expiration = moment("2017-01-29T17:24:22+0000");
let diff = expiration.diff(now);
let diffDuration = moment.duration(diff);
let dayDiff = diffDuration.days() + "d";
let hoursDiff = diffDuration.hours() + "hrs";
let minDiff = diffDuration.minutes() + "m";
console.log(`${dayDiff} ${hoursDiff} ${minDiff}`);

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

How to convert timestap to hours and minutes in angularjs?

I want to count how many hours and minutes between two timestamp which are generated by Date.parse. After I get the difference, I need to convert it into hours and minutes like 2.10 (means, 2 hours and 10 minutes). Once I have read that to do it you need to divide it with 3600 so I tried this code but it just gives me 0.89 instead of 1.26.
var now = new Date();
var endTime = Date.parse(now)/1000;
var startTime = Date.parse("2018-03-16 10:29:17")/1000;
$scope.timestamp_difference = startTime - endTime;
$scope.hours = $scope.timestamp_difference/3600;
How to do it right?
In case you haven't heard, Momentjs makes working with dates and times pretty damn easy in javascript i updated code for you may hope it will helps you now
var date1 = moment('03/15/2018 11:00', 'MM/DD/YYYY hh:mm'),
date2 = moment('03/16/2018 10:00', 'MM/DD/YYYY hh:mm');
var duration = moment.duration(date2.diff(date1));
//you will get 23 hours 00 minute
alert(duration.asHours().toFixed(2))
http://jsfiddle.net/dp7rzmw5/9771/
Output: 23:00 hrs
function getTimeDifference(timestampDifference) {
var hours = ~~(timestampDifference / 3600);
var minuts = ~~((timestampDifference - hours * 3600) / 60);
var seconds = timestampDifference - hours * 3600 - minuts * 60;
return hours + '.' + minuts + '.' + seconds;
}
var minutes = "0" + Math.floor(timestampDifference / 60);
// get minutes
var seconds = "0" + Math.floor(timestampDifference - minutes * 60);
// get seconds
var hour = "0" + Math.floor(timestampDifference / 3600);
//get hour
if ( hour > 1 ) {
return hour.substr(-2) + ":" + minutes.substr(-2) + ":" + seconds.substr(-2);
} else {
return minutes.substr(-2) + ":" + seconds.substr(-2);
}
// check if the hour is greater then 1 than return time with hour else return minutes.

Angularjs Django compare dates

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"

Resources