Skip to content
Closed
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
16 changes: 16 additions & 0 deletions Client-Side Components/Client Scripts/Date Helpers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# ServiceNow JavaScript Helpers

A set of lightweight JavaScript helper functions for use in ServiceNow Script Includes, Business Rules, and Client Scripts.
No external dependencies, just pure JavaScript.

---

## 📅 Date Helpers (`dateHelpers.js`)

- `getTodayISO()` → Returns today's date in `YYYY-MM-DD`
- `getTodayReadable()` → Returns today's date in human-readable string (`Sat Oct 04 2025`)
- `getCurrentYear()` → Returns current year as a number
- `formatUnixTimestamp(unixSeconds)` → Converts UNIX timestamp into `{ fullDateTime, date, time }`
- `formatDateTime(jsDate)` → Converts JS Date/string into `"YYYY-MM-DD HH:mm:ss"`

---
39 changes: 39 additions & 0 deletions Client-Side Components/Client Scripts/Date Helpers/dateHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Returns today's date in YYYY-MM-DD format
function getTodayISO() {
var d = new Date();
return d.getFullYear() + "-" +
((d.getUTCMonth() + 1) < 10 ? "0" + (d.getUTCMonth() + 1) : (d.getUTCMonth() + 1)) + "-" +
(d.getDate() < 10 ? "0" + d.getDate() : d.getDate());
};

// Returns today's date in human-readable string
function getTodayReadable() {
return new Date().toDateString();
};

// Returns only the current year
function getCurrentYear() {
return new Date().getFullYear();
};

// Converts UNIX timestamp (in seconds) to date + time info
function formatUnixTimestamp(unixSeconds) {
var d = new Date(unixSeconds * 1000);
return {
fullDateTime: d.toDateString() + " at " + d.toLocaleTimeString(),
date: d.toDateString(),
time: d.toLocaleTimeString(),
};
};

// Converts JS Date or date string to "YYYY-MM-DD HH:mm:ss"
function formatDateTime(inputDate) {
var d = new Date(inputDate);
var date = d.getFullYear() + "-" +
((d.getUTCMonth() + 1) < 10 ? "0" + (d.getUTCMonth() + 1) : (d.getUTCMonth() + 1)) + "-" +
(d.getDate() < 10 ? "0" + d.getDate() : d.getDate());
var time = (d.getHours() < 10 ? "0" + d.getHours() : d.getHours()) + ":" +
(d.getMinutes() < 10 ? "0" + d.getMinutes() : d.getMinutes()) + ":" +
(d.getSeconds() < 10 ? "0" + d.getSeconds() : d.getSeconds());
return date + " " + time;
};
19 changes: 19 additions & 0 deletions Client-Side Components/Client Scripts/String Helpers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# ServiceNow JavaScript Helpers

A set of lightweight JavaScript helper functions for use in ServiceNow Script Includes, Business Rules, and Client Scripts.
No external dependencies, just pure JavaScript.

---

## ✨ String Helpers (`stringHelpers.js`)

- `toSlug("Hello World!")` → `"hello-world"`
- `fromSlugToWords("hello-world")` → `"hello world"`
- `fromSlugToCompact("hello-world")` → `"helloworld"`
- `toUnderscoreCase("hello world")` → `"hello_world"`
- `capitalizeWords("hello world")` → `"Hello World"`
- `toUpperCase("hello")` → `"HELLO"`
- `toLowerCase("HELLO")` → `"hello"`
- `trimText(" hello ")` → `"hello"`

---
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Converts string to lowercase, removes symbols, replaces spaces with dashes
function toSlug(text) {
let str = text.toLowerCase();
str = str.replace(/[^a-z0-9_\s-]/g, "");
str = str.replace(/[\s-]+/g, " ");
str = str.replace(/[\s_]/g, "-");
return str;
};

// Converts dashed/underscored text back to words with spaces
function fromSlugToWords(text) {
return text.replace(/[-_]/g, " ");
};

// Converts dashed/underscored text to continuous word
function fromSlugToCompact(text) {
return text.replace(/[-_]/g, "");
};

// Replaces spaces with underscores
function toUnderscoreCase(text) {
return text.replace(/\s/g, "_");
};

// Capitalizes the first letter of each word
function capitalizeWords(text) {
return text.toLowerCase().replace(/(^\w{1})|(\s+\w{1})/g, (letter) => letter.toUpperCase());
};

// Converts entire string to uppercase
function toUpperCase(text) {
return text ? text.toUpperCase() : text;
};

// Converts entire string to lowercase
function toLowerCase(text) {
return text ? text.toLowerCase() : text;
};

// Removes spaces from start and end
function trimText(text) {
return text.trim();
};
Loading