📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
In this chapter, we will explore the Date.getMilliseconds()
method in TypeScript. This method returns the milliseconds (from 0 to 999) of the specified date according to local time. Understanding how to use Date.getMilliseconds()
is useful for extracting the milliseconds from a date object.
Table of Contents
- Definition
- Syntax
- Examples
- Conclusion
1. Definition
The Date.getMilliseconds()
method returns the milliseconds (from 0 to 999) of the specified date according to local time.
2. Syntax
dateObj.getMilliseconds();
Parameters
The Date.getMilliseconds()
method does not take any parameters.
Return Value
The method returns an integer number, between 0 and 999, representing the milliseconds for the given date according to local time.
3. Examples
Let's look at some examples to understand how Date.getMilliseconds()
works in TypeScript.
Example 1: Basic Usage
In this example, we use Date.getMilliseconds()
to get the milliseconds from a specific date.
let date = new Date('2024-07-15T10:20:30.123Z');
let milliseconds = date.getMilliseconds();
console.log(milliseconds); // Output: 123
Example 2: Getting the Current Milliseconds
In this example, we use Date.getMilliseconds()
to get the current milliseconds.
let now = new Date();
let currentMilliseconds = now.getMilliseconds();
console.log(currentMilliseconds); // Output: The current milliseconds, e.g., 456
Example 3: Using Date.getMilliseconds()
with Different Times
In this example, we use Date.getMilliseconds()
to get the milliseconds for different times.
let morning = new Date('2024-07-15T08:00:00.789');
let evening = new Date('2024-07-15T20:00:00.456');
console.log(morning.getMilliseconds()); // Output: 789
console.log(evening.getMilliseconds()); // Output: 456
Example 4: Using Date.getMilliseconds()
with a Date Object
In this example, we use Date.getMilliseconds()
with a date object created from the current date and time.
let date = new Date();
let milliseconds = date.getMilliseconds();
console.log(milliseconds); // Output: The current milliseconds
Example 5: Using Date.getMilliseconds()
with Past and Future Dates
In this example, we use Date.getMilliseconds()
to get the milliseconds for past and future dates.
let pastDate = new Date('2000-01-01T12:34:56.789');
let futureDate = new Date('2050-05-20T23:45:00.012');
console.log(pastDate.getMilliseconds()); // Output: 789
console.log(futureDate.getMilliseconds()); // Output: 12
Example 6: Using Date.getMilliseconds()
in Different Time Zones
In this example, we use Date.getMilliseconds()
to get the milliseconds for a date object in different time zones.
let date = new Date('2024-07-15T10:20:30.123Z');
let millisecondsUTC = date.getUTCMilliseconds();
let millisecondsLocal = date.getMilliseconds();
console.log(millisecondsUTC); // Output: 123
console.log(millisecondsLocal); // Output: 123 (same for UTC and local as milliseconds are not affected by time zones)
4. Conclusion
In this chapter, we explored the Date.getMilliseconds()
method in TypeScript, which is used to return the milliseconds (from 0 to 999) of the specified date according to local time. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use Date.getMilliseconds()
effectively can help in various scenarios where extracting the milliseconds from a date object is required.
Comments
Post a Comment
Leave Comment