Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow different date formatting like YYYY/MM/DD with Moment.js library. #31

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ The configuration:

- `customStylesheetURL` - (*string*, default to `null`) Path to a custom stylesheet file, for those who doesn't like the default *theme*.
- `yearLength` - (*number*, default to `120`) The width of the year grids, in pixels.
- `dateFormats` - (*array*, default to ["YYYY", "MM/YYYY", "DD/MM/YYYY"]) Available date formats. You can change it to ["YYYY", "YYYY/MM", "YYYY/MM/DD"].

Datetime "syntax"
-----------------
Expand Down
3 changes: 2 additions & 1 deletion config.example.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"customStylesheetURL": null,
"yearLength": 120,
"hideAge": false
"hideAge": false,
"dateFormats": ["YYYY", "MM/YYYY", "DD/MM/YYYY"]
}
21 changes: 8 additions & 13 deletions index.html
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ <h1 id="title">Life</h1>
<a href="https://github.com/cheeaun/life">Fork me</a>
</header>
<div id="life"></div>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.4.0/moment.min.js"></script>
<script>
(function(){
var life = {
Expand Down Expand Up @@ -177,22 +178,16 @@ <h1 id="title">Life</h1>
var data = {};
if (/^\~\d+$/.test(time)){ // ~YYYY
data = {
startYear: parseInt(time.slice(1), 10),
startYear: moment(time.slice(1), "YYYY", true).year(),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single quotes please :)

estimate: true
};
} else if (/^\d+$/.test(time)){ // YYYY
data[point + 'Year'] = parseInt(time, 10);
} else if (/^\d+\/\d+$/.test(time)){ // MM/YYYY
var t = time.split('/');
data[point + 'Month'] = parseInt(t[0], 10);
data[point + 'Year'] = parseInt(t[1], 10);
} else if (/^\d+\/\d+\/\d+$/.test(time)){ // DD/MM/YYYY
var t = time.split('/');
data[point + 'Date'] = parseInt(t[0], 10);
data[point + 'Month'] = parseInt(t[1], 10);
data[point + 'Year'] = parseInt(t[2], 10);
} else if(moment(time, life.config.dateFormats, true).isValid()){
var t = moment(time, life.config.dateFormats);
data[point + 'Date'] = t.day();
data[point + 'Month'] = t.month();
data[point + 'Year'] = t.year();
} else if (/\d\-/.test(time)){ // TIME-TIME
var splitTime = time.split('-');
var splitTime = time.split('-'); //bug We cannot use 2013-12-04 format!
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could introduce another option for a custom separator but let's leave it like this first :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes definitely, we will need to use a different separator.

var startTime = life.parseTime(splitTime[0]);
var endTime = life.parseTime(splitTime[1], 'end');
for (var k in startTime) { data[k] = startTime[k] }
Expand Down
Loading