Lightning Component to show Date, Time and Greeting
Lightning Aura Component that shows current Date and Time with Greeting.
Screenshot: -
Component: -
ViewDateTimeAndGreeting.cmp
- <aura:component implements="flexipage:availableForAllPageTypes">
- <aura:handler name="init" value="{!this}" action="{!c.handleTime}" />
- <aura:attribute name="hour" type="Integer" />
- <aura:attribute name="minute" type="Integer" />
- <aura:attribute name="period" type="String" />
- <aura:attribute name="day" type="String" />
- <aura:attribute name="date" type="Integer" />
- <aura:attribute name="month" type="String" />
- <aura:attribute name="year" type="String" />
- <aura:attribute name="greet" type="String" default="Hello !" />
- <aura:attribute name="currentUser" type="User"/>
- <force:recordData aura:id="recordLoader" recordId="{!$SObjectType.CurrentUser.Id}" fields="Name" targetFields="{!v.currentUser}"/>
- <div class="slds-card slds-p-around_xx-small main-container">
- <div class="slds-grid">
- <div class="slds-col slds-size_1-of-5">
- <lightning:icon iconName="utility:clock" variant="inverse" size="small" alternativeText="Time"/>
- </div>
- <div class="slds-col slds-size_4-of-5 slds-text-heading_large slds-text-align_center">
- <div>{!v.hour} : {!v.minute} {!v.period}</div>
- <div>{!v.greet}, {!v.currentUser.Name}</div>
- </div>
- </div>
- <div class="slds-card slds-p-around_xx-small slds-m-top_x-small date-container">
- <div class="slds-grid">
- <div class="slds-col slds-size_1-of-5">
- <lightning:icon iconName="utility:event" variant="inverse" size="small" alternativeText="Date"/>
- </div>
- <div class="slds-col slds-size_4-of-5 slds-text-align_center">
- <div class="slds-text-heading_small">{!v.day}, {!v.date} {!v.month} {!v.year}</div>
- </div>
- </div>
- </div>
- </div>
- </aura:component>
- ({
- handleTime : function(component, event, helper) {
- var today = new Date();
- var hour = today.getHours();
- var period = hour >= 12 ? 'PM' : 'AM';
- helper.setGreet(component,hour);
- if (hour > 12) {
- hour -= 12;
- } else if (hour === 0) {
- hour = 12;
- }
- var minute = today.getMinutes();
- minute = helper.checkDigit(minute); //will check if minute is single digit then add '0' before it
- var date = today.getDate();
- var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
- var dayName = days[today.getDay()];
- var month = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
- var monthName = month[today.getMonth()];
- var year = today.getFullYear();
- component.set("v.hour", hour);
- component.set("v.minute", minute);
- component.set("v.period", period);
- component.set("v.day", dayName);
- component.set("v.date", date);
- component.set("v.month", monthName);
- component.set("v.year", year);
- },
- })
- ({
- checkDigit : function(minute) {
- if (minute < 10) {minute = "0" + minute}; // add zero in front of single digit minute
- return minute;
- },
- setGreet : function(component,hour) {
- if (hour <= 11) {
- component.set("v.greet", "Good morning");
- } else if (hour >= 12 && hour <= 15) {
- component.set("v.greet", "Good afternoon");
- } else if (hour == 20 && hour < 21) {
- component.set("v.greet", "It's time to go home");
- } else {
- component.set("v.greet", "Good evening");
- }
- },
- })
- .THIS.main-container{
- background-color : #3498DB;
- color : #fff;
- }
- .THIS .date-container{
- background-color : #5DADE2;
- color : #fff;
- }


Comments
Post a Comment