**
**
// 当天日期
const today = new Date( ) ;
// 格式化当天日期为 YYYY-MM-DD 格式
const formattedToday = today.toISOString( ) .slice( 0 , 10 ) ;
// 昨天日期
const yesterday = new Date( ) ;
yesterday.setDate( yesterday.getDate( ) - 1 ) ;
// 格式化昨天日期为 YYYY-MM-DD 格式
const formattedYesterday = yesterday.toISOString( ) .slice( 0 , 10 ) ;
console.log( "当天日期:" , formattedToday) ;
console.log( "昨天日期:" , formattedYesterday) ;
数组转json,json转数组
以下是将数组转换为JSON字符串的示例代码:
const array = [ 1 , 2 , 3 , 4 , 5 ] ;
const jsonString = JSON.stringify( array) ;
console.log( jsonString) ; // 输出:"[1,2,3,4,5]"
在这个示例中,数组[ 1 , 2 , 3 , 4 , 5 ] 通过调用JSON.stringify( ) 方法转换为JSON字符串"[1,2,3,4,5]" 。
以下是将JSON字符串转换为数组的示例代码:
const jsonString = '[1, 2, 3, 4, 5]' ;
const array = JSON.parse( jsonString) ;
console.log( array) ; // 输出:[ 1 , 2 , 3 , 4 , 5 ]
在这个示例中,JSON字符串'[1, 2, 3, 4, 5]' 通过调用JSON.parse( ) 方法转换为数组[ 1 , 2 , 3 , 4 , 5 ] 。
请注意,传递给JSON.parse( ) 方法的字符串必须是有效的JSON格式,否则将引发语法错误。确保提供的字符串符合JSON规范,包括正确的语法、引号使用等。