Home Web Front-end HTML Tutorial JS time object and reference type

JS time object and reference type

Mar 08, 2018 pm 02:45 PM
javascript Quote type

This time I will bring you JS time objects and reference types. What are the precautions when using JS time objects and reference types? Here are practical cases, let’s take a look.

What are the basic types? What are the complex types? What are the characteristics?
Basic types: String type, Null type, Number type, Undefined type, Boolean type
Complex type: Object type
Function:
String type: String is a sequence of Unicode characters, commonly known as String , can be expressed by double quotes or single quotes, there is no difference, just match
Null type: The Null type has only one value: null, which represents a null pointer, that is, something that does not exist
Number Type: JavaScript’s number type is different from other languages. There is no difference between integers and floating point numbers. They are all Number types, which can represent decimal, octal, and hexadecimal
Undefined types: The Undefined type also has only one value, undefined, which means that the variable has only been declared and not initialized, that is, there is this pointer, but this pointer does not point to any space
Boolean type: Boolean has two values: 1.true2.false
Object class type: Object (object) is the core concept of JavaScript and the most important data type. All data in JavaScript can be regarded as objects, which is why we often say that everything is an object.

The output of the following code? Why?

var obj1 = {a:1, b:2};
var obj2 = {a:1, b:2};
console.log(obj1 == obj2);//false,由于obj1与obj2所储存的位置不同,所以false。
console.log(obj1 = obj2);//obj2赋值给obj1 输出 Object { a=1,  b=2} 内容。
console.log(obj1 == obj2);//把obj2赋值给obj1,所以obj2与obj1存储的位置是一样的,所以为true。
Copy after login

Code

Write a function getIntv to get the interval from the current time to the specified date.
var str = getIntv("2016-01-08");
console.log(str); // There are still 20 days, 15 hours, 20 minutes and 10 seconds until New Year's Eve
Code:

var str = getIntv("2017-01-27");
function getIntv(time){
var end = new Date(time);
var now = new Date();
var timer = end-now;
var day = Math.floor(timer/(1000606024));
var timer1 = timer%(1000606024)
var hour = Math.floor(timer1/(10006060));
var timer2 = timer1%(10006060);
var min = Math.floor(timer2/(100060));
var timer3 = timer2%(100060);
var sec = Math.floor(timer3/1000);
return ("距"+time+"还有"+day+"天"+hour+"小时"+min+"分钟"+sec+"秒")
}
console.log(str);  // 距2017-01-27还有 20 天 15 小时 20 分 10 秒
Copy after login

Change the digital date to Chinese date, such as:

var str = getChsDate('2015-01-08');
console.log(str);  // 二零一五年一月八日
Copy after login

Code:
Method one:

var str = getChsDate('2015-01-08');
function getChsDate(time){
time = time.replace(/-/g,'');
var arr = []
for(i=0;i<time.length;i++){
switch(time[i]){
case &#39;0&#39;: arr.push(&#39;零&#39;);break;
case &#39;1&#39;: arr.push(&#39;一&#39;);break;
case &#39;2&#39;: arr.push(&#39;二&#39;);break;
case &#39;3&#39;: arr.push(&#39;三&#39;);break;
case &#39;4&#39;: arr.push(&#39;四&#39;);break;
case &#39;5&#39;: arr.push(&#39;五&#39;);break;
case &#39;6&#39;: arr.push(&#39;六&#39;);break;
case &#39;7&#39;: arr.push(&#39;七&#39;);break;
case &#39;8&#39;: arr.push(&#39;八&#39;);break;
case &#39;9&#39;: arr.push(&#39;九&#39;);break;
}
}
console.log(time);
arr.splice(4,0,&#39;年&#39;);
arr.splice(7,0,&#39;月&#39;);
arr.splice(10,0,&#39;日&#39;);
arr = arr.join(&#39;&#39;);
return arr;
}//这种方法有一定缺陷,比如&#39;2016-02-28&#39;,输出&#39;二零一六年零二月二八日&#39;,读起来很别扭
console.log(str);  // 二零一五年一月八日
Copy after login

Method two:

function getChsDate(date){
var newDate =date.split("-"),
year = newDate[0],
month = newDate[1],
day = newDate[2];
var dict ={"0":"零","1": "一", "2": "二", "3": "三","4": "四","5": "五","6": "六","7": "七", "8": "八", "9": "九", "10": "十", "11": "十一", "12": "十二","13": "十三", "14": "十四",  "15": "十五", "16": "十六", "17": "十七", "18": "十八", "19": "十九","20": "二十","21": "二十一", "22": "二十二", "23": "二十三", "24": "二十四",  "25": "二十五","26": "二十六", "27": "二十七", "28": "二十八", "29": "二十九", "30": "三十", "31": "三十一"};
return dict[year[0]]+dict[year[1]]+dict[year[2]]+dict[year[3]] + &#39;年&#39; + dict[Number(month)] +&#39;月&#39; + dict[Number(day)] + &#39;日&#39;;
};
getChsDate(&#39;2015-01-08&#39;);//"二零一五年一月八日"
Copy after login

Write a function to get the date n days ago:

var lastWeek =  getLastNDays(7); // ‘2016-01-08’
  var lastMonth = getLastNDays(30); //&#39;2015-12-15&#39;
Copy after login

Code:

var lastWeek =  getLastNDays(7); // ‘2016-01-08’
var lastMonth = getLastNDays(30); //&#39;2015-12-15&#39;
function getLastNDays(dater){
var now = Date.now();
var timer = dater2460601000;
var past = new Date(now - timer);
var year = past.getFullYear();
var month = past.getMonth()+1;//月份从0开始算;
var day = past.getDate();
return year+&#39;-&#39;+month+&#39;-&#39;+day;
}
console.log(lastWeek);
console.log(lastMonth);
Copy after login

Complete the following code, such as:

var Runtime = (function(){
//code here ...
return {
start: function(){
//code here ...
},
end: function(){
//code here ...
},
get: function(){
//code here ...
}
};
}());
Runtime.start();
//todo somethint
Runtime.end();
console.log(  Runtime.get() );
Copy after login

Code:

var Runtime = (function(){
var time1;
var time2;
return {
start: function(){
time1=Date.now();
},
end: function(){
time2=Date.now();
},
get: function(){
return (time2-time1);
}
};
}());
Runtime.start();
for(var i=0;i<100;i++){
console.log(1);//输出100次1
}
Runtime.end();
console.log(  Runtime.get() );//运行了22ms
Copy after login

There are 200 steps in the stairs. Each time you take 1 or 2 levels, how many ways are there in total from the bottom to the top? Use code (recursively) to implement

function fn(num) {
if (num==0||num==1) {
return 1;
}
else {
return fn(num-1)+fn(num-2);
}
}
console.log(fn(200));
Copy after login

Writing a method of deep copying a json object. The json object can be nested in multiple layers, and the value can be a string, number, Boolean, or any item in the json object

var json={
"name":"yahoo",
"age":"14",
"sex":"man",
"address":
{
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
}
}
function JSON(arr){
var newjson= {};
for(key in arr){
if(typeof arr[key]=="object"){
newjson[key]=JSON(arr[key]);
}
else{
newjson[key]=arr[key];
}
}
return newjson;
}
console.log(JSON(json))
Copy after login

Write a deep copy method of an array. The values ​​in the array can be strings, numbers, Boolean, or any items in the array.

var arr=[1,"2",3,[1,2,3,4],true]
function JSON(arr){
var newarr=[];
for(key in arr){
if(typeof arr[key] ==&#39;Array&#39;) {
newarr[key]=JSON(arr[key]);
}
else{
newarr[key]=arr[key];
}
}
return newarr;
}
console.log(JSON(arr))
Copy after login

Write a deep copy method Method, copy object and internal nested value can be any item in string, number, Boolean, array, json object

var O={
name:"yahoo",
age:14,
other:[1,2,true,"yahoo",3],
man:{
"man1":"woman",
"man2":"man2"
},
aid:true,
address:
{
streetAddress: "21 2nd Street",
city: "New York",
state: "NY",
postalCode: "10021"
}
}
function JOSN(O){
var newarr={};
for(key in O){
if (typeof O[key] ===&#39;Array&#39;){
newarr[key]=JOSN(O[key]);
}
else{
newarr[key]=O[key];
}
}
return newarr;
}
console.log(JOSN(O))
Copy after login

I believe you have mastered the method after reading the case in this article, more Please pay attention to other related articles on the php Chinese website!

Related reading:

Simple bubble and two-way bubble sorting case

Javascript used to download images script

The above is the detailed content of JS time object and reference type. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

What are the benefits of C++ functions returning reference types? What are the benefits of C++ functions returning reference types? Apr 20, 2024 pm 09:12 PM

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.

How to create a video matrix account? What types of matrix accounts do it have? How to create a video matrix account? What types of matrix accounts do it have? Mar 21, 2024 pm 04:57 PM

With the popularity of short video platforms, video matrix account marketing has become an emerging marketing method. By creating and managing multiple accounts on different platforms, businesses and individuals can achieve goals such as brand promotion, fan growth, and product sales. This article will discuss how to effectively use video matrix accounts and introduce different types of video matrix accounts. 1. How to create a video matrix account? To make a good video matrix account, you need to follow the following steps: First, you must clarify what the goal of your video matrix account is, whether it is for brand communication, fan growth or product sales. Having clear goals helps develop strategies accordingly. 2. Choose a platform: Choose an appropriate short video platform based on your target audience. The current mainstream short video platforms include Douyin, Kuaishou, Huoshan Video, etc.

How to use C++ reference and pointer parameter passing? How to use C++ reference and pointer parameter passing? Apr 12, 2024 pm 10:21 PM

References and pointers in C++ are both methods of passing function parameters, but there are differences. A reference is an alias for a variable. Modifying the reference will modify the original variable, while the pointer stores the address of the variable. Modifying the pointer value will not modify the original variable. When choosing to use a reference or a pointer, you need to consider factors such as whether the original variable needs to be modified, whether a null value needs to be passed, and performance considerations.

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

See all articles