Home > Web Front-end > Front-end Q&A > How to change format in javascript

How to change format in javascript

PHPz
Release: 2023-04-24 14:26:17
Original
1034 people have browsed it

JavaScript is a dynamic language that is widely used in web development, mobile applications, game development and other fields. In the development process of JavaScript, format conversion is a very common operation, such as converting string format to date format, converting object format to JSON format, encrypting or decrypting data, etc.

This article will introduce several common format conversion operations, and give implementation code and usage examples.

1. Convert string to date format

In web applications, it is often necessary to convert time in string format to date format and perform related calculations and comparisons. JavaScript provides a Date object, and we can use this object to process dates.

The following is the code to convert the string format to date format:

function stringToDate(dateStr) {
  var date = new Date(dateStr.replace(/-/g, '/'));
  return date;
}
Copy after login

By using the replace method, we replace the "-" character with the "/" character, and then use new Date() Function parses a string into a date object.

Usage example:

var dateString = "2021-10-19";
var date = stringToDate(dateString);
console.log(date);
Copy after login

Output result:

Tue Oct 19 2021 00:00:00 GMT+0800 (中国标准时间)
Copy after login

2. Convert object format to JSON format

In web applications, objects often need to be The formatted data is converted into JSON format for transmission and storage. JSON objects are provided in JavaScript, and we can use the JSON.stringify() method to serialize the object into a JSON-formatted string.

The following is the code to convert the object format into JSON format:

function objectToJson(object) {
  var json = JSON.stringify(object);
  return json;
}
Copy after login

Usage example:

var obj = {name: "Tom", age: 20, gender: "male"};
var json = objectToJson(obj);
console.log(json);
Copy after login

Output result:

{"name":"Tom","age":20,"gender":"male"}
Copy after login

3. Data encryption and Decryption

In web applications, sensitive data often needs to be encrypted to protect it during network transmission. Several encryption algorithms are provided in JavaScript, such as MD5, SHA-1, AES, etc. We can choose different encryption algorithms according to specific needs.

The following is the code to encrypt a string using the MD5 algorithm:

function md5Encrypt(str) {
  var md5 = require('md5');
  var encryptedStr = md5(str);
  return encryptedStr;
}
Copy after login

In order to use the MD5 algorithm, we need to introduce the md5 library, here we use the require() function to introduce it.

Usage example:

var str = "hello world";
var encryptedStr = md5Encrypt(str);
console.log(encryptedStr);
Copy after login

Output result:

5eb63bbbe01eeed093cb22bb8f5acdc3
Copy after login

When transmitting or storing data, we also need to decrypt the data. The following is the code to decrypt a string encrypted by the MD5 algorithm:

function md5Decrypt(encryptedStr) {
  // MD5算法不支持解密操作,所以这里略过
  return "";
}
Copy after login

Since the MD5 algorithm does not support the decryption operation, we return an empty string.

To sum up, JavaScript provides a wealth of format conversion and encryption algorithms, which we can choose and use according to specific needs. In actual development, we need to consider issues such as data security, efficiency, and maintainability in order to provide better services to users.

The above is the detailed content of How to change format in javascript. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template