javascript notes
javascript notes
<!doctype html>
<head>
<title>Create User-defined Object in JavaScript</title>
</head>
<body>
<script>
/* defining a new constructor function */
function Bike(company, model, year) {
this.company = company;
this.model = model;
this.year = year;
}
Let's see how we can create the Date object in JavaScript. Although
there are multiple ways of doing it, the default way is using the new keyword
with the Date constructor function.
1588693792007
In the code above we have used the static function now() for the Date
object. This will give us the Unix timestamp for current date and time which
nothing but the number of milliseconds passed since 1 January 1970.
Apart from the above two syntaxes, there are multiple other ways to
create a date in JavaScript. Here, we are using different syntax to create da
objects.
<!doctype html>
<head>
<title>JS Date Example</title>
</head>
<body>
<script>
/* JS comes here */
let date1 = new Date('February 13, 1991 06:44:00');
let date2 = new Date('1991-02-13T06:44:00');
let date3 = new Date(1991, 02, 13); // the month is 0 indexe
let date4 = new Date(1991, 02, 13, 6, 44, 0);
document.write("Example 1: " + date1);
document.write("<br/>Example 2: " + date2);
document.write("<br/>Example 3: " + date3);
document.write("<br/>Example 4: " + date4);
</script>
</body>
</html>
As you can see in the examples above, we can create a date for a
custom date which can be a past date or a future date. We can even provid
the time value.
Working with JavaScript Date
To get the value for the year, month or day from a date, we can use
JavaScript built-in methods. These methods are easy to use and reduce the
effort and time for coding.
// Creating a date
let date1 = new Date(2018,11,25,12,25,12,0);
// Getting year
let year = date1.getFullYear();
document.write("<br> Year: "+ year);
// Getting Month
let month = date1.getMonth();
document.write("<br> Month: "+ month);
// Getting day
let day = date1.getDate();
document.write("<br> Day: "+ day);
Copy
Year: 2018
Month: 11
Day: 25
JavaScript provides a rich set of built-in methods which are both stati
and instance methods that can be used to get different information from
JavaScript Date object.
Method Description
setDate() sets the date of the month that ranges from 1 to 31.
Now we are going to show you how to use some of the Date Object
methods:
<html>
<head>
<title>
Working with Date object
</title>
</head>
<body>
<script type="text/javaScript">
var mydate = new Date();
document.write("Today date is: " + mydate.getDate()+"/"+
(mydate.getMonth()+1)+"/"+mydate.getFullYear()+"<br/>");
document.write("The time is: " + mydate.getHours()
+":"+mydate.getMinutes()+":"+mydate.getSeconds()+"<br/>");
</script>
</body>
</html>
We can simply subtract two dates to find the difference between any t
dates. Let's take an example to see this:
// Using Date objects
let start = Date.now();
// Literal Syntax
let regExp = /pattern/flag;
Copy
Where the pattern is the text of regular expression, the flag makes t
regular expression search for a specific pattern.
Note: In this tutorial, we will discuss the RegExp object, its properties
and methods. All the examples will be based on the RegExp object, not the
literal regular expression. We have covered the regex literal syntax earlier.
We will be using the following String methods to use the RegExp objec
to test, match, and validate various string values in JavaScript:
test()
match()
replace()
exec()
Copy
Ip address is valid
Shyam, Mohan
Fetch Subdomain Value from a Domain Name
linux
Find a Pattern in String
To find a pattern in string, we can use the string match() method. It retu
the match pattern that can be a substring. See the below example:
// Creating regex object
var regex = new RegExp(/India/g);
var str = "We live in India.";
// search for India
var matches = str.match(regex);
document.write(matches);
Copy
India
The ignoreCase property indicates whether or not the i flag is used with th
regular expression. The ignoreCase is a read-only property of the RegExp obje
var re = new RegExp("/hello/","i");
// ignorecase property
var result = re.ignoreCase;
document.write(result);
Copy
true