Mahlatse
Mahlatse

Reputation: 121

d3 dateformat parse returns null

I am using d3.time.format to parse a time variable. But the parsed_date returns null value.

date = "Thu Apr 18 09:06:23 +0000 2019"
dateFormat = d3.time.format("%Y-%m-%d")
parsed_date = dateFormat.parse(date)

I expect

parsed_date to return "2019-04-18"

Upvotes: 1

Views: 182

Answers (1)

Mikhail Shabrikov
Mikhail Shabrikov

Reputation: 8509

.parse method allows to convert date as string to native JavaScript date format. After that you can format the date as you want, so you should rewrite you code this way to make it works:

const date = "Thu Apr 18 09:06:23 +0000 2019";

const parseFunc = d3.time.format("%a %b %d %H:%M:%S %Z %Y").parse;
const formatFunc = d3.time.format("%Y-%m-%d");

const parsed_date = formatFunc(parseFunc(date));

console.log(parsed_date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>

Upvotes: 1

Related Questions