Skip to content
Related Articles
Open in App
Not now

Related Articles

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

Improve Article
Save Article
  • Last Updated : 05 Jan, 2023
Improve Article
Save Article

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.


Input:  today
Output: SUN

Input: tomorrow
Output: MON

Input: yesterday
Output: SAT

Input: 01-04-2021
Output: THU


  • 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 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
    // Function calls



My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!