假设我有this JSON响应,并且我只想从AddedDate
属性中检索日期(而不是时间),或者在正确< / strong>方式(dd-mm-yy)。如何在纯JS中做到这一点?
答案 0 :(得分:0)
var json=[
{
"Name": "BattlefieldHeroes",
"Title": "Battlefield Heroes",
"Domain": "battlefieldheroes.com",
"BreachDate": "2011-06-26",
"AddedDate": "2014-01-23T13:10:29Z",
"ModifiedDate": "2014-01-23T13:10:29Z",
"PwnCount": 530270,
"Description": "In June 2011 as part of a final breached data dump, the hacker collective "LulzSec" <a href=\"http:\/\/www.rockpapershotgun.com\/2011\/06\/26\/lulzsec-over-release-battlefield-heroes-data\" target=\"_blank\" rel=\"noopener\">obtained and released over half a million usernames and passwords from the game Battlefield Heroes<\/a>. The passwords were stored as MD5 hashes with no salt and many were easily converted back to their plain text versions.",
"LogoType": "svg",
"DataClasses": [
"Passwords",
"Usernames"
],
"IsVerified": true,
"IsFabricated": false,
"IsSensitive": false,
"IsRetired": false,
"IsSpamList": false
},
{
"Name": "BusinessAcumen",
"Title": "Business Acumen Magazine",
"Domain": "businessacumen.biz",
"BreachDate": "2014-04-25",
"AddedDate": "2014-05-11T04:25:48Z",
"ModifiedDate": "2014-05-11T04:25:48Z",
"PwnCount": 26596,
"Description": "In April 2014, the Australian \"Business Acumen Magazine\" website was <a href=\"http:\/\/1337mir.com\/cracked\/2014\/04\/businessacumen-biz-hacked-26000-user-password-leaked\/\" target=\"_blank\" rel=\"noopener\">hacked by an attacker known as 1337MiR<\/a>. The breach resulted in over 26,000 accounts being exposed including usernames, email addresses and password stored with a weak cryptographic hashing algorithm (MD5 with no salt).",
"LogoType": "svg",
"DataClasses": [
"Email addresses",
"Names",
"Passwords",
"Usernames",
"Website activity"
],
"IsVerified": true,
"IsFabricated": false,
"IsSensitive": false,
"IsRetired": false,
"IsSpamList": false
},
{
"Name": "Forbes",
"Title": "Forbes",
"Domain": "forbes.com",
"BreachDate": "2014-02-15",
"AddedDate": "2014-02-15T11:24:42Z",
"ModifiedDate": "2014-02-15T11:24:42Z",
"PwnCount": 1057819,
"Description": "In February 2014, the Forbes website <a href=\"http:\/\/news.cnet.com\/8301-1009_3-57618945-83\/syrian-electronic-army-hacks-forbes-steals-user-data\" target=\"_blank\" rel=\"noopener\">succumbed to an attack that leaked over 1 million user accounts<\/a>. The attack was attributed to the Syrian Electronic Army, allegedly as retribution for a perceived \"Hate of Syria\". The attack not only leaked user credentials, but also resulted in the posting of fake news stories to forbes.com.",
"LogoType": "svg",
"DataClasses": [
"Email addresses",
"Passwords",
"User website URLs",
"Usernames"
],
"IsVerified": true,
"IsFabricated": false,
"IsSensitive": false,
"IsRetired": false,
"IsSpamList": false
},
{
"Name": "Gawker",
"Title": "Gawker",
"Domain": "gawker.com",
"BreachDate": "2010-12-11",
"AddedDate": "2013-12-04T00:00:00Z",
"ModifiedDate": "2013-12-04T00:00:00Z",
"PwnCount": 1247574,
"Description": "In December 2010, Gawker was attacked by the hacker collective "Gnosis" in retaliation for what was reported to be a feud between Gawker and 4Chan. Information about Gawkers 1.3M users was published along with the data from Gawker's other web presences including Gizmodo and Lifehacker. Due to the prevalence of password reuse, many victims of the breach <a href=\"http:\/\/www.troyhunt.com\/2011\/01\/why-your-apps-security-design-could.html\" target=\"_blank\" rel=\"noopener\">then had their Twitter accounts compromised to send Acai berry spam<\/a>.",
"LogoType": "svg",
"DataClasses": [
"Email addresses",
"Passwords",
"Usernames"
],
"IsVerified": true,
"IsFabricated": false,
"IsSensitive": false,
"IsRetired": false,
"IsSpamList": false
},
{
"Name": "Hemmelig",
"Title": "hemmelig.com",
"Domain": "hemmelig.com",
"BreachDate": "2011-12-21",
"AddedDate": "2014-03-25T07:23:52Z",
"ModifiedDate": "2014-03-25T07:23:52Z",
"PwnCount": 28641,
"Description": "In December 2011, Norway's largest online sex shop hemmelig.com was <a href=\"http:\/\/www.dazzlepod.com\/hemmelig\/?page=93\" target=\"_blank\" rel=\"noopener\">hacked by a collective calling themselves "Team Appunity"<\/a>. The attack exposed over 28,000 usernames and email addresses along with nicknames, gender, year of birth and unsalted MD5 password hashes.",
"LogoType": "svg",
"DataClasses": [
"Email addresses",
"Genders",
"Nicknames",
"Passwords",
"Usernames",
"Years of birth"
],
"IsVerified": true,
"IsFabricated": false,
"IsSensitive": false,
"IsRetired": false,
"IsSpamList": false
},
{
"Name": "LoungeBoard",
"Title": "Lounge Board",
"Domain": "loungeboard.net",
"BreachDate": "2013-08-01",
"AddedDate": "2014-07-06T10:22:01Z",
"ModifiedDate": "2014-07-06T10:22:01Z",
"PwnCount": 45018,
"Description": "At some point in 2013, 45k accounts were <a href=\"http:\/\/leak.sx\/thread-186921\" target=\"_blank\" rel=\"noopener\">breached from the Lounge Board \"General Discussion Forum\" and then dumped publicly<\/a>. Lounge Board was a MyBB forum launched in 2012 and discontinued in mid 2013 (the last activity in the logs was from August 2013).",
"LogoType": "svg",
"DataClasses": [
"Email addresses",
"IP addresses",
"Names",
"Passwords",
"Private messages",
"Usernames",
"Website activity"
],
"IsVerified": true,
"IsFabricated": false,
"IsSensitive": false,
"IsRetired": false,
"IsSpamList": false
},
{
"Name": "PokemonCreed",
"Title": "Pok\u00e9mon Creed",
"Domain": "pokemoncreed.net",
"BreachDate": "2014-08-08",
"AddedDate": "2014-08-10T00:03:59Z",
"ModifiedDate": "2014-08-10T00:03:59Z",
"PwnCount": 116465,
"Description": "In August 2014, the Pok\u00e9mon RPG website <a href=\"http:\/\/pokemoncreed.net\" target=\"_blank\" rel=\"noopener\">Pok\u00e9mon Creed<\/a> was hacked after a dispute with rival site, <a href=\"http:\/\/pkmndusk.in\" target=\"_blank\" rel=\"noopener\">Pok\u00e9mon Dusk<\/a>. In a <a href=\"https:\/\/www.facebook.com\/ramandeep.s.dehal\/posts\/749666358442465\" target=\"_blank\" rel=\"noopener\">post on Facebook<\/a>, "Cruz Dusk" announced the hack then pasted the dumped MySQL database on <a href=\"http:\/\/pkmndusk.in\" target=\"_blank\" rel=\"noopener\">pkmndusk.in<\/a>. The breached data included over 116k usernames, email addresses and plain text passwords.",
"LogoType": "svg",
"DataClasses": [
"Email addresses",
"Genders",
"IP addresses",
"Passwords",
"Usernames",
"Website activity"
],
"IsVerified": true,
"IsFabricated": false,
"IsSensitive": false,
"IsRetired": false,
"IsSpamList": false
},
{
"Name": "Verified",
"Title": "Verified",
"Domain": "verified.cm",
"BreachDate": "2014-01-10",
"AddedDate": "2014-07-06T04:16:37Z",
"ModifiedDate": "2014-07-06T04:16:37Z",
"PwnCount": 16919,
"Description": "In January 2014, <a href=\"http:\/\/securityaffairs.co\/wordpress\/21120\/cyber-crime\/verified-communities-hacked.html\" target=\"_blank\" rel=\"noopener\">one of the largest communities of Eastern Europe cybercriminals known as \"Verified\" was hacked<\/a>. The breach exposed nearly 17k users of the vBulletin forum including their personal messages and other potentially personally identifiable information.",
"LogoType": "svg",
"DataClasses": [
"Email addresses",
"Historical passwords",
"IP addresses",
"Passwords",
"Private messages",
"Usernames",
"Website activity"
],
"IsVerified": true,
"IsFabricated": false,
"IsSensitive": false,
"IsRetired": false,
"IsSpamList": false
},
{
"Name": "Win7Vista",
"Title": "Win7Vista Forum",
"Domain": "win7vista.com",
"BreachDate": "2013-09-03",
"AddedDate": "2014-06-01T10:01:32Z",
"ModifiedDate": "2014-06-01T10:01:32Z",
"PwnCount": 202683,
"Description": "In September 2013, the <a href=\"http:\/\/www.win7vista.com\" target=\"_blank\" rel=\"noopener\">Win7Vista<\/a> Windows forum (since renamed to the \"Beyond Windows 9\" forum) was hacked and later <a href=\"http:\/\/leak.sx\/thread-186933\" target=\"_blank\" rel=\"noopener\">had its internal database dumped<\/a>. The dump included over 200k members\u2019 personal information and other internal data extracted from the forum.",
"LogoType": "svg",
"DataClasses": [
"Email addresses",
"Instant messenger identities",
"IP addresses",
"Names",
"Passwords",
"Private messages",
"Usernames",
"Website activity"
],
"IsVerified": true,
"IsFabricated": false,
"IsSensitive": false,
"IsRetired": false,
"IsSpamList": false
}
]
for(var i=0;i<json.length;i++){
var strDate = json[i].AddedDate;
console.log(strDate.substring(0, 10))
}
var json=[
{
"Name": "BattlefieldHeroes",
"Title": "Battlefield Heroes",
"Domain": "battlefieldheroes.com",
"BreachDate": "2011-06-26",
"AddedDate": "2014-01-23T13:10:29Z",
"ModifiedDate": "2014-01-23T13:10:29Z",
"PwnCount": 530270,
"Description": "In June 2011 as part of a final breached data dump, the hacker collective "LulzSec" <a href=\"http:\/\/www.rockpapershotgun.com\/2011\/06\/26\/lulzsec-over-release-battlefield-heroes-data\" target=\"_blank\" rel=\"noopener\">obtained and released over half a million usernames and passwords from the game Battlefield Heroes<\/a>. The passwords were stored as MD5 hashes with no salt and many were easily converted back to their plain text versions.",
"LogoType": "svg",
"DataClasses": [
"Passwords",
"Usernames"
],
"IsVerified": true,
"IsFabricated": false,
"IsSensitive": false,
"IsRetired": false,
"IsSpamList": false
},
{
"Name": "BusinessAcumen",
"Title": "Business Acumen Magazine",
"Domain": "businessacumen.biz",
"BreachDate": "2014-04-25",
"AddedDate": "2014-05-11T04:25:48Z",
"ModifiedDate": "2014-05-11T04:25:48Z",
"PwnCount": 26596,
"Description": "In April 2014, the Australian \"Business Acumen Magazine\" website was <a href=\"http:\/\/1337mir.com\/cracked\/2014\/04\/businessacumen-biz-hacked-26000-user-password-leaked\/\" target=\"_blank\" rel=\"noopener\">hacked by an attacker known as 1337MiR<\/a>. The breach resulted in over 26,000 accounts being exposed including usernames, email addresses and password stored with a weak cryptographic hashing algorithm (MD5 with no salt).",
"LogoType": "svg",
"DataClasses": [
"Email addresses",
"Names",
"Passwords",
"Usernames",
"Website activity"
],
"IsVerified": true,
"IsFabricated": false,
"IsSensitive": false,
"IsRetired": false,
"IsSpamList": false
},
{
"Name": "Forbes",
"Title": "Forbes",
"Domain": "forbes.com",
"BreachDate": "2014-02-15",
"AddedDate": "2014-02-15T11:24:42Z",
"ModifiedDate": "2014-02-15T11:24:42Z",
"PwnCount": 1057819,
"Description": "In February 2014, the Forbes website <a href=\"http:\/\/news.cnet.com\/8301-1009_3-57618945-83\/syrian-electronic-army-hacks-forbes-steals-user-data\" target=\"_blank\" rel=\"noopener\">succumbed to an attack that leaked over 1 million user accounts<\/a>. The attack was attributed to the Syrian Electronic Army, allegedly as retribution for a perceived \"Hate of Syria\". The attack not only leaked user credentials, but also resulted in the posting of fake news stories to forbes.com.",
"LogoType": "svg",
"DataClasses": [
"Email addresses",
"Passwords",
"User website URLs",
"Usernames"
],
"IsVerified": true,
"IsFabricated": false,
"IsSensitive": false,
"IsRetired": false,
"IsSpamList": false
},
{
"Name": "Gawker",
"Title": "Gawker",
"Domain": "gawker.com",
"BreachDate": "2010-12-11",
"AddedDate": "2013-12-04T00:00:00Z",
"ModifiedDate": "2013-12-04T00:00:00Z",
"PwnCount": 1247574,
"Description": "In December 2010, Gawker was attacked by the hacker collective "Gnosis" in retaliation for what was reported to be a feud between Gawker and 4Chan. Information about Gawkers 1.3M users was published along with the data from Gawker's other web presences including Gizmodo and Lifehacker. Due to the prevalence of password reuse, many victims of the breach <a href=\"http:\/\/www.troyhunt.com\/2011\/01\/why-your-apps-security-design-could.html\" target=\"_blank\" rel=\"noopener\">then had their Twitter accounts compromised to send Acai berry spam<\/a>.",
"LogoType": "svg",
"DataClasses": [
"Email addresses",
"Passwords",
"Usernames"
],
"IsVerified": true,
"IsFabricated": false,
"IsSensitive": false,
"IsRetired": false,
"IsSpamList": false
},
{
"Name": "Hemmelig",
"Title": "hemmelig.com",
"Domain": "hemmelig.com",
"BreachDate": "2011-12-21",
"AddedDate": "2014-03-25T07:23:52Z",
"ModifiedDate": "2014-03-25T07:23:52Z",
"PwnCount": 28641,
"Description": "In December 2011, Norway's largest online sex shop hemmelig.com was <a href=\"http:\/\/www.dazzlepod.com\/hemmelig\/?page=93\" target=\"_blank\" rel=\"noopener\">hacked by a collective calling themselves "Team Appunity"<\/a>. The attack exposed over 28,000 usernames and email addresses along with nicknames, gender, year of birth and unsalted MD5 password hashes.",
"LogoType": "svg",
"DataClasses": [
"Email addresses",
"Genders",
"Nicknames",
"Passwords",
"Usernames",
"Years of birth"
],
"IsVerified": true,
"IsFabricated": false,
"IsSensitive": false,
"IsRetired": false,
"IsSpamList": false
},
{
"Name": "LoungeBoard",
"Title": "Lounge Board",
"Domain": "loungeboard.net",
"BreachDate": "2013-08-01",
"AddedDate": "2014-07-06T10:22:01Z",
"ModifiedDate": "2014-07-06T10:22:01Z",
"PwnCount": 45018,
"Description": "At some point in 2013, 45k accounts were <a href=\"http:\/\/leak.sx\/thread-186921\" target=\"_blank\" rel=\"noopener\">breached from the Lounge Board \"General Discussion Forum\" and then dumped publicly<\/a>. Lounge Board was a MyBB forum launched in 2012 and discontinued in mid 2013 (the last activity in the logs was from August 2013).",
"LogoType": "svg",
"DataClasses": [
"Email addresses",
"IP addresses",
"Names",
"Passwords",
"Private messages",
"Usernames",
"Website activity"
],
"IsVerified": true,
"IsFabricated": false,
"IsSensitive": false,
"IsRetired": false,
"IsSpamList": false
},
{
"Name": "PokemonCreed",
"Title": "Pok\u00e9mon Creed",
"Domain": "pokemoncreed.net",
"BreachDate": "2014-08-08",
"AddedDate": "2014-08-10T00:03:59Z",
"ModifiedDate": "2014-08-10T00:03:59Z",
"PwnCount": 116465,
"Description": "In August 2014, the Pok\u00e9mon RPG website <a href=\"http:\/\/pokemoncreed.net\" target=\"_blank\" rel=\"noopener\">Pok\u00e9mon Creed<\/a> was hacked after a dispute with rival site, <a href=\"http:\/\/pkmndusk.in\" target=\"_blank\" rel=\"noopener\">Pok\u00e9mon Dusk<\/a>. In a <a href=\"https:\/\/www.facebook.com\/ramandeep.s.dehal\/posts\/749666358442465\" target=\"_blank\" rel=\"noopener\">post on Facebook<\/a>, "Cruz Dusk" announced the hack then pasted the dumped MySQL database on <a href=\"http:\/\/pkmndusk.in\" target=\"_blank\" rel=\"noopener\">pkmndusk.in<\/a>. The breached data included over 116k usernames, email addresses and plain text passwords.",
"LogoType": "svg",
"DataClasses": [
"Email addresses",
"Genders",
"IP addresses",
"Passwords",
"Usernames",
"Website activity"
],
"IsVerified": true,
"IsFabricated": false,
"IsSensitive": false,
"IsRetired": false,
"IsSpamList": false
},
{
"Name": "Verified",
"Title": "Verified",
"Domain": "verified.cm",
"BreachDate": "2014-01-10",
"AddedDate": "2014-07-06T04:16:37Z",
"ModifiedDate": "2014-07-06T04:16:37Z",
"PwnCount": 16919,
"Description": "In January 2014, <a href=\"http:\/\/securityaffairs.co\/wordpress\/21120\/cyber-crime\/verified-communities-hacked.html\" target=\"_blank\" rel=\"noopener\">one of the largest communities of Eastern Europe cybercriminals known as \"Verified\" was hacked<\/a>. The breach exposed nearly 17k users of the vBulletin forum including their personal messages and other potentially personally identifiable information.",
"LogoType": "svg",
"DataClasses": [
"Email addresses",
"Historical passwords",
"IP addresses",
"Passwords",
"Private messages",
"Usernames",
"Website activity"
],
"IsVerified": true,
"IsFabricated": false,
"IsSensitive": false,
"IsRetired": false,
"IsSpamList": false
},
{
"Name": "Win7Vista",
"Title": "Win7Vista Forum",
"Domain": "win7vista.com",
"BreachDate": "2013-09-03",
"AddedDate": "2014-06-01T10:01:32Z",
"ModifiedDate": "2014-06-01T10:01:32Z",
"PwnCount": 202683,
"Description": "In September 2013, the <a href=\"http:\/\/www.win7vista.com\" target=\"_blank\" rel=\"noopener\">Win7Vista<\/a> Windows forum (since renamed to the \"Beyond Windows 9\" forum) was hacked and later <a href=\"http:\/\/leak.sx\/thread-186933\" target=\"_blank\" rel=\"noopener\">had its internal database dumped<\/a>. The dump included over 200k members\u2019 personal information and other internal data extracted from the forum.",
"LogoType": "svg",
"DataClasses": [
"Email addresses",
"Instant messenger identities",
"IP addresses",
"Names",
"Passwords",
"Private messages",
"Usernames",
"Website activity"
],
"IsVerified": true,
"IsFabricated": false,
"IsSensitive": false,
"IsRetired": false,
"IsSpamList": false
}
]
答案 1 :(得分:0)
对于添加的日期字段,您可以使用regex函数在字符串内部查找数字并解构数组以获取相应的字段
let [year, month, day] = object["AddedDate"].match(/\d+/g);
console.log(day, month, year) //returns 15 02 2014
然后您可以根据需要设置日期格式
console.log(`${day}-${month}-${year.substr(2)}`) //returns 15-02-14
答案 2 :(得分:0)
看一下文档 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC
var timestamp = Date.parse('2014-01-23T13:10:29Z')
然后,您可以使用返回的时间戳做任何您想做的事情
date = new Date(timestamp);
console(date.getFullYear());
答案 3 :(得分:0)
我们想在reviver函数中为日期补水,传递给JSON.parse()。它将处理您的刚刚约会和您的iso日期。它在这里工作...
function nReviver(key, value) {
var a;
if (typeof value === "string") {
// match any string that looks like a date, with or without a time. YYYY-MM-DD(Thh:mm:ss(.0000)?Z?)?
a = /^((\d{4})-(\d{2})-(\d{2}))(T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z?)?$/.exec(value);
if (a) {
// No default values. All zero dates replaced with null.
if (a[1] === '0001-01-01') { return null; }
// sby JSON.net loves datetimes with timezone unspecified. We prefer them as UTC.
if (value.slice(-1) !== 'Z')
value += 'Z';
return new Date(Date.UTC(+a[2], +a[3] - 1, +a[4], +a[5] || 0, +a[6] || 0, +a[7] || 0));
}
}
return value;
}
var json = '{"Name":"BattlefieldHeroes","Title":"Battlefield Heroes","Domain":"battlefieldheroes.com","BreachDate":"2011-06-26","AddedDate":"2014-01-23T13:10:29Z","ModifiedDate":"2014-01-23T13:10:29Z","PwnCount":530270,"Description":"In June 2011 as part of a final breached data dump, the hacker collective "LulzSec" <a href=\\"http://www.rockpapershotgun.com/2011/06/26/lulzsec-over-release-battlefield-heroes-data\\" target=\\"_blank\\" rel=\\"noopener\\">obtained and released over half a million usernames and passwords from the game Battlefield Heroes</a>. The passwords were stored as MD5 hashes with no salt and many were easily converted back to their plain text versions.","LogoType":"svg","DataClasses":["Passwords","Usernames"],"IsVerified":true,"IsFabricated":false,"IsSensitive":false,"IsRetired":false,"IsSpamList":false}'
var rehydrated = JSON.parse(json,nReviver)
console.log(rehydrated)