JavaScript Date
What is the Date Object?
The Date object in JavaScript is used to work with dates and times. It provides methods for creating, formatting, manipulating, and comparing dates.
Creating Date Objects
1. Current Date and Time
let now = new Date();
console.log(now); // Output: current date and time
2. Specific Date
let birthday = new Date("1995-12-17");
console.log(birthday); // Output: Sun Dec 17 1995 ...
3. With Year, Month, Day, etc.
let customDate = new Date(2023, 0, 15, 10, 30); // Jan is 0
console.log(customDate); // Output: Sun Jan 15 2023 10:30:00 ...
Date Methods
Method | Description | Example Output |
---|---|---|
getFullYear( ) | Gets the year | 2024 |
getMonth() | Gets the month (0–11) | 0 for January |
getDate() | Gets the day of the month (1–31) | 15 |
getDay() | Gets the day of the week (0–6) | 0 for Sunday |
getHours() | Gets hours (0–23) | 10 |
getMinutes() | Gets minutes (0–59) | 30 |
getSeconds() | Gets seconds (0–59) | 45 |
getTime() | Gets time in milliseconds since 1970 | 1700000000000 (example) |
toDateString () | Converts to readable date string | "Mon Jan 15 2024" |
toISOString( ) | Converts to ISO 8601 format | "2024-01-15T08:30:00.00 0Z" |
Example: Get Current Components
let today = new Date();
console.log(today.getFullYear()); // Output: 2024
console.log(today.getMonth()); // Output: 0 (Jan)
console.log(today.getDate()); // Output: 15
Modifying Dates
let date = new Date();
date.setFullYear(2030);
date.setMonth(11); // December
console.log(date); // Output: updated date
JavaScript RegExp (Regular Expressions)
What is a RegExp?
A Regular Expression (RegExp) is a pattern used to match character combinations in strings.
It is commonly used for searching, validating, and replacing text.
Syntax
let pattern = /abc/;
let result = pattern.test("abc123");
console.log(result); // Output: true
You can also use the constructor:
let pattern = new RegExp("abc");
Common RegExp Methods
Method | Description | Example Output |
---|---|---|
test() | Returns true if match is found | true |
exec() | Returns match object or null | [ 'abc' ] |
match() | Returns match (used with strings) | ["abc"] |
replace( ) | tdReplaces matched text | str.replace() |
search() | Returns index of match | 5 |
split() | Splits string by pattern | str.split() |
Basic RegExp Patterns
Pattern | Description | Matches |
---|---|---|
. | Any single character | "a" , "1" |
\d | Any digit (0–9) | "3" , "7" |
\w | Any word character (a–z, A–Z, 0–9, _) | "a" , "Z" , "0" , " _ " |
\s | Whitespace | space, tab, newline |
^abc | Starts with "abc" | "abc123" |
abc$ | Ends with "abc" | "123abc" |
` a | b` | "a" or "b" |
[abc] | Any of a, b, or c | "a" , "b" |
[^abc] | Not a, b, or c | "d" , "x" |
a* | 0 or more a's | "" , "a" , "aaaa" |
a+ | 1 or more a's | "a" , "aa" |
a? | 0 or 1 a | "" , "a" |
{n} | Exactly n times | a{3} matches "aaa" |
{n,} | At least n times | a{2,} matches "aa" , "aaa" |
{n,m} | Between n and m times | a{2,4} matches "aa" , "aaa" |
Example: Email Validation
let email =
"test@example.com";
let pattern = /^[\w.
-]+@[\w.
-]+\.\w+$/;
console.log(pattern.test(email)); // Output: true
Example: Replace Digits
let str =
"abc123";
let result = str.replace(/\d/g,
"*");
console.log(result); // Output: abc***
Example: Split by Space
let sentence =
"JavaScript is awesome";
let words = sentence.split(/\s/);
console.log(words); // Output: ["JavaScript", "is", "awesome"]
Recap: RegExp vs Date
Feature | Date | RegExp |
---|---|---|
Use Case | Work with time and calendar | Pattern matching in strings |
Constructor | new Date() | new RegExp() or /pattern/ |
Output Type | Object | Boolean or Array |