Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavascriptHow to get the first three characters of a string using JavaScript...

How to get the first three characters of a string using JavaScript ?

The following approach covers how to find the 3 letters of the string, here we will use the day’s name as a string by using JavaScript for a given date.

These are the approaches by which we can get the first three characters of a string using JavaScript:

Example:

Input:  today
Output: SUN

Input: tomorrow
Output: MON

Input: yesterday
Output: SAT

Input: 01-04-2021
Output: THU

Approach 1: Using Slice() method

  • First, get the current date using the new Date( ) and store it in a variable (date).
  • If the input date is tomorrow the using setDate() method increases one date to the Date(), if the input date is yesterday the decrease one date from the Date().
  • And if the value is not today then we pass the input date to the Date() object. That’s why if the user inputs today then the Date() will be the default and the default Date() object depicts today’s Date().
  • Now by using the date.getDay() method we get the day of the date. It will return a number ranging from 0-6 where 0 represents Sunday and 6 represents Saturday and the remaining days lie in order. Create an array containing the days of the week. By using the index get the preferred day name.
  • By using the slice method extract the Beginning three characters.

Example: This example shows the first three letters of the current day.

Javascript




// Javascript program to get three  beginning characters of the day
 
function getDay(d) {
    let date = new Date();
    let days = ["SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY",
        "THURSDAY", "FRIDAY", "SATURDAY"]
 
    if (d === "tomorrow") {
        date.setDate(date.getDate() + 1)
    } else if (d === "yesterday") {
        date.setDate(date.getDate() - 1)
    } else if (d != "today") {
        date = new Date(d);
    }
 
    // Get the todays day
    let day = days[date.getDay()]
 
    // Extract three characters from the beginning
    let threeCharDay = day.slice(0, 3)
 
    // Print or return the three character day
    console.log(threeCharDay)
}
 
// Function calls
getDay("yesterday")
getDay("today")
getDay("tomorrow")
getDay("2021-03-30")
getDay("2021-03-31")
getDay("2021-04-01")


Output

SUN
MON
TUE
TUE
WED
THU

Approach 2: Using string.substr() method

Syntax: 

let temp = str.substr(0, 3); 

Example: This example shows the first three letters of the current day using string.substr() method.

Javascript




// Javascript program to get three beginning characters of the day
 
function getDay(d) {
    let date = new Date();
    let days = ["SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY",
        "THURSDAY", "FRIDAY", "SATURDAY"]
 
    if (d === "tomorrow") {
        date.setDate(date.getDate() + 1)
    } else if (d === "yesterday") {
        date.setDate(date.getDate() - 1)
    } else if (d != "today") {
        date = new Date(d);
    }
 
    // Get the todays day
    let day = days[date.getDay()]
 
    // Extract three characters from the beginning
    let threeCharDay = day.substr(0, 3)
 
    // Print or return the three character day
    console.log(threeCharDay)
}
 
// Function calls
getDay("yesterday")
getDay("today")
getDay("tomorrow")
getDay("2021-03-30")
getDay("2021-03-31")
getDay("2021-04-01")


Output

SUN
MON
TUE
TUE
WED
THU

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments