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

jQuery #7

Merged
merged 5 commits into from
Mar 15, 2024
Merged
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: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<!-- default badges list -->
![](https://img.shields.io/endpoint?url=https://codecentral.devexpress.com/api/v1/VersionRange/744047123/23.2.3%2B)
[![](https://img.shields.io/badge/Open_in_DevExpress_Support_Center-FF7200?style=flat-square&logo=DevExpress&logoColor=white)](https://supportcenter.devexpress.com/ticket/details/T1211517)
[![](https://img.shields.io/badge/📖_How_to_use_DevExpress_Examples-e9f6fc?style=flat-square)](https://docs.devexpress.com/GeneralInformation/403183)
<!-- default badges end -->
Expand Down
18 changes: 18 additions & 0 deletions jQuery/src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,21 @@ const employees = [{
Address: '807 W Paseo Del Mar',
StateID: 5,
}];

const items = [
{
text: 'Century cuts off at 50 years',
value: 'javascript',
},
{
text: 'Century cuts off after current decade',
value: 'excel',
},
{
text: 'Century cuts off at current year',
value: 'past',
},
{
text: 'No century cut-off',
value: 'nocutoff',
}];
4 changes: 2 additions & 2 deletions jQuery/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
width: 90vh;
}

#grid {
margin-top: 50px;
#parse-behavior, #date {
margin-bottom: 50px;
}
1 change: 1 addition & 0 deletions jQuery/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<body class="dx-viewport">
<div class="demo-container">
<div id="parse-behavior"></div>
<div id="date"></div>
<div id="grid"></div>
</div>
Expand Down
8 changes: 8 additions & 0 deletions jQuery/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
$(() => {
const now = new Date();

const parseSelectBox = $('#parse-behavior').dxSelectBox({
width: 300,
items,
displayExpr: 'text',
valueExpr: 'value',
value: 'javascript',
}).dxSelectBox('instance');

$('#date').dxDateBox({
type: 'date',
label: 'Date with the short year',
Expand Down
38 changes: 33 additions & 5 deletions jQuery/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
const parser = (value) => {
function parser(value) {
const parseSelectBox = $('#parse-behavior').dxSelectBox('instance');

const algorithm = parseSelectBox.option('value');
const resultDate = new Date(value);

Choose a reason for hiding this comment

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

I suggest to pass the algorithm through a parameter similar to what you did here. This way, the utility methods are separate from the main app.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good point, thanks!


if (algorithm === 'javascript') { return resultDate; }

const parts = value.split('/');
if (parts.length !== 3) return value;
if (parts.length !== 3) { return value; }

let year = Number(parts[2]);
if (year < 100) year += 2000;
return new Date(year, Number(parts[0]) - 1, Number(parts[1]));
};
if (year < 100) {
year = getFourDigitYear(year, algorithm);
resultDate.setFullYear(year);
}
return resultDate;
}

function getFourDigitYear(twoDigitYear, algorithm) {
const now = new Date();
const yearToday = now.getFullYear();
const centuryToday = yearToday - (yearToday % 100);
let fullYear = centuryToday + twoDigitYear;

if (algorithm === 'nocutoff') { return fullYear; }

let currentCenturyCutOff = yearToday;
if (algorithm === 'excel') {
const nextDecadeStart = yearToday - (yearToday % 10) + 10;
currentCenturyCutOff = nextDecadeStart - 1;
}
if (fullYear > currentCenturyCutOff) { fullYear -= 100; }

return fullYear;
}
const formatter = (value) => value.toLocaleDateString();
Loading