Skip to content

Add datetime formatting capability to variable set task #777

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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,4 @@ _Pvt_Extensions
/.cr
/*/v[0-9]/*.js
/*/v[0-9]/*.js.map
./*/v[0-9]/*.tsbuildinfo
1 change: 1 addition & 0 deletions vsts-variable-set/v1/tsconfig.tsbuildinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"root":["./vsts-variable-set.ts"],"version":"5.8.3"}
1 change: 1 addition & 0 deletions vsts-variable-set/v2/tsconfig.tsbuildinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"root":["./vsts-variable-set.ts"],"version":"5.8.3"}
12 changes: 11 additions & 1 deletion vsts-variable-set/v3/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"type": "pickList",
"options": {
"value": "value",
"env": "env"
"env": "env",
"datetime": "current date/time"
}
},
{
Expand All @@ -82,6 +83,15 @@
"aliases": ["Env", "Environment"],
"visibleRule": "From=env"
},
{
"defaultValue": "yyyy-MM-dd HH:mm:ss",
"helpMarkdown": "Format string for the current date and time. Supported patterns: yyyy (4-digit year), yy (2-digit year), MM (2-digit month), dd (2-digit day), HH (24-hour), hh (12-hour), mm (minute), ss (second), tt (AM/PM). Example: 'yyyy-MM-dd HH:mm:ss' produces '2023-12-25 14:30:00'.",
"label": "Date/Time Format",
"name": "DateTimeFormat",
"required": true,
"type": "string",
"visibleRule": "From=datetime"
},
{
"defaultValue": false,
"helpMarkdown": "Save variable as a secret.",
Expand Down
1 change: 1 addition & 0 deletions vsts-variable-set/v3/tsconfig.tsbuildinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"root":["./vsts-variable-set.ts"],"version":"5.8.3"}
46 changes: 46 additions & 0 deletions vsts-variable-set/v3/vsts-variable-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,47 @@

const variable = tl.getInput("VariableName", true);

function formatDateTime(format: string): string {
const now = new Date();

// Simple and robust approach - only support explicit multi-character patterns
// This avoids conflicts with single characters in normal text
let formatted = format;

// Year patterns
formatted = formatted.replace(/yyyy/g, now.getFullYear().toString());
formatted = formatted.replace(/yy/g, now.getFullYear().toString().slice(-2));

// Month patterns (2-digit and single digit with leading zero requirement)
const month = (now.getMonth() + 1).toString().padStart(2, '0');
formatted = formatted.replace(/MM/g, month);

// Day patterns
const day = now.getDate().toString().padStart(2, '0');
formatted = formatted.replace(/dd/g, day);

// Hour 24-hour patterns
const hour = now.getHours().toString().padStart(2, '0');
formatted = formatted.replace(/HH/g, hour);

// Hour 12-hour patterns
const hour12 = (now.getHours() % 12 || 12).toString().padStart(2, '0');
formatted = formatted.replace(/hh/g, hour12);

// Minute patterns
const minute = now.getMinutes().toString().padStart(2, '0');
formatted = formatted.replace(/mm/g, minute);

// Second patterns
const second = now.getSeconds().toString().padStart(2, '0');
formatted = formatted.replace(/ss/g, second);

// AM/PM patterns
formatted = formatted.replace(/tt/g, now.getHours() >= 12 ? 'PM' : 'AM');

return formatted;
}

function getValue()
{
const from = tl.getInput("From") || "value";
Expand All @@ -15,6 +56,11 @@ function getValue()
{
return process.env[tl.getInput("Env", true)];
}
case "datetime":
{
const format = tl.getInput("DateTimeFormat", true) || "yyyy-MM-dd HH:mm:ss";
return formatDateTime(format);
}
default:
{
return "";
Expand Down
1 change: 1 addition & 0 deletions vsts-variable-transform/v1/tsconfig.tsbuildinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"root":["./vsts-variable-transform.ts"],"version":"5.8.3"}
1 change: 1 addition & 0 deletions vsts-variable-transform/v2/tsconfig.tsbuildinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"root":["./vsts-variable-transform.ts"],"version":"5.8.3"}
1 change: 1 addition & 0 deletions vsts-variable-transform/v3/tsconfig.tsbuildinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"root":["./vsts-variable-transform.ts"],"version":"5.8.3"}