Many pages show dates, count down to events, or process names and messages. JavaScript provides the Date object and many string methods.

The Date Object

A Date object represents a moment in time. new Date() gives the current date and time, new Date(2026, 11, 25) a given date (months count from 0, so 11 is December), and new Date("2026-12-25") parses a string. Methods read the parts: getFullYear(), getMonth() (0 to 11), getDate() (day of month), getDay() (day of week, 0 for Sunday), getHours(), getMinutes() and getSeconds(). Matching set methods change them. toDateString(), toLocaleDateString() and toLocaleTimeString() give readable text. Internally a date is a number of milliseconds since 1 January 1970, from getTime(), so subtracting two dates gives the difference in milliseconds, and dividing by 86,400,000 gives days.

Definition: Date object. A built-in JavaScript object that represents a single moment in time and provides methods to read, set, compare and format dates and times.

Strings

A string is a sequence of characters in quotes. Strings are immutable: methods return new strings. length gives the number of characters. toUpperCase() and toLowerCase() change case. trim() removes spaces at the ends. indexOf() and includes() search. slice(start, end) and substring() extract parts. replace() and replaceAll() substitute text. split(separator) breaks a string into an array, and the array's join() puts it back. charAt(i) gives one character, and padStart() pads it, as for "05". startsWith() and endsWith() test the ends. Template literals in back quotes build strings with ${expressions}.

Common Tasks

With these tools, scripts can show a live clock with setInterval, count the days to an exam, check whether a word is a palindrome with split, reverse and join, count the words in a text with split, and capitalise names.

Table 3.6: Useful Date and String methods

Method Gives Example
getFullYear() Year 2026
getMonth() Month 0–11 10 for November
getDay() Weekday 0–6 4 for Thursday
split(" ") Array of words "a b".split(" ")
slice(0, 3) Part of a string "Java".slice(0, 3) = "Jav"

Example 3.6: Days to an exam and string tricks

Problem: Find the number of days from 26 November 2026 to an exam on 15 December 2026, and for the text " web technologies is fun " give the trimmed upper-case text, the word count, and whether "level" is a palindrome.

Solution:

  1. Create both dates, subtract them and divide by 86,400,000: 19 days.
  2. trim() and toUpperCase() clean the text; split(" ") gives 4 words.
  3. "level" reversed is "level", so it is a palindrome.

Result: 19 days; WEB TECHNOLOGIES IS FUN; 4 words; level is a palindrome.

const today = new Date(2026, 10, 26);          // months count from 0
const exam = new Date(2026, 11, 15);
const days = (exam - today) / 86400000;
console.log("days to exam:", days, "| weekday no.:", today.getDay());
console.log(today.toDateString());
const text = "  web technologies is fun  ";
const clean = text.trim();
console.log(clean.toUpperCase(), "| words:", clean.split(" ").length);
const w = "level";
const rev = w.split("").reverse().join("");
const verdict = w === rev ? "palindrome" : "no";
console.log(`${w} reversed is ${rev}:`, verdict);
console.log("Java".slice(0, 3), "BCA".padStart(5, "*"),
            "abc".includes("b"));

Output

days to exam: 19 | weekday no.: 4
Thu Nov 26 2026
WEB TECHNOLOGIES IS FUN | words: 4
level reversed is level: palindrome
Jav **BCA true

Common mistakes

  • Forgetting that months start at 0, so new Date(2026, 12, 1) is January 2027.
  • Confusing getDate() (day of month) with getDay() (day of week).
  • Expecting a string method to change the original string.

Key takeaways

  • new Date() gives now; months run from 0 to 11.
  • Subtracting dates gives milliseconds; divide by 86,400,000 for days.
  • Strings are immutable; methods return new strings.
  • split, join, slice, replace and trim handle most text tasks.

Practice questions

  1. What does getDay() return?
  2. How do you find the number of days between two dates?
  3. What does split(" ") do to a string?

Answers to the practice questions

  1. The day of the week, from 0 (Sunday) to 6 (Saturday).
  2. Subtract them and divide the milliseconds by 86,400,000.
  3. It breaks it into an array of words at each space.

Sources

  • Official BCA syllabus Web Technologies, course code 25BCA402SEC02, Unit III