
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
01-09-2019 09:16 AM - edited 06-20-2025 07:09 AM
Scripts::. GlideDate / GetDate / Get Month Name / Exemplo Como obter a data e hora atual
A manipulação de datas e horários é uma tarefa recorrente em scripts da plataforma ServiceNow, especialmente ao lidar com fluxos de trabalho, relatórios e automações. Este artigo apresenta exemplos práticos utilizando as classes GlideDate
e GlideDateTime
, além de comparações com JavaScript puro.
Handling dates and times is a recurring task in ServiceNow platform scripts, especially when dealing with workflows, reports, and automations. This article presents practical examples using the GlideDate
and GlideDateTime
classes, as well as comparisons with plain JavaScript.
✅ Como obter a data e hora atual
// Retorna a data e hora atual no formato de exibição do usuário
var nowDateTime = gs.nowDateTime(); // Ex: 05/01/2018 10:14:03
gs.print("nowDateTime: " + nowDateTime);
// Retorna apenas a data no formato do usuário
var nowDate = gs.now(); // Ex: 05/01/2018
gs.print("nowDate: " + nowDate);
// Retorna a data como objeto GlideDate (sem hora)
var gd = new GlideDate(); // Ex: 2018-01-05
gs.print("GlideDate (gd): " + gd.getValue());
// Retorna data e hora em formato local (string para exibição)
var displayDateTime = new GlideDateTime().getDisplayValue(); // Ex: 05/01/2018 10:25:26
gs.print("displayDateTime: " + displayDateTime);
// Retorna o dia da semana (1 = Domingo, 7 = Sábado)
var dow = gd.getDayOfWeek(); // Ex: 5 para quinta-feira
gs.print("Dia da semana (dow): " + dow);
// Formato de data do usuário (ex: dd/MM/yyyy)
var userDateFormat = gs.getDateFormat();
gs.print("Formato de data do usuário: " + userDateFormat);
sample
*** Script: nowDateTime: 2025-06-18 14:09:44
*** Script: nowDate: 2025-06-18
*** Script: GlideDate (gd): 2025-06-18
*** Script: displayDateTime: 2025-06-18 14:09:44
*** Script: Dia da semana (dow): 3
*** Script: Formato de data do usuário: yyyy-MM-dd
📆 Obter ano, mês e dia da data atual
Year Month day
var ObjDate = new GlideDateTime();
var dt = ObjDate.getDisplayValue();
year = ObjDate.getYearUTC();
month = ObjDate.getMonthUTC();
day = ObjDate.getDayOfMonthUTC();
gs.print(dt); //09/01/2019 15:09:35
gs.print(year); //2019
gs.print(month); //1
gs.print(day); //09
⏱️ Obter hora, minutos e segundos com JavaScript puro
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1; // Atenção: getMonth() retorna de 0 a 11
var day = now.getDate();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
gs.print('Data atual: ' + now);
gs.print('Hora: ' + hours + ' Min: ' + minutes + ' Seg: ' + seconds);
📤 Separando Data e Hora
var gdt = new GlideDateTime('2025-12-12 08:00:00');
var d = gdt.getDate().getValue(); //Gets the date of the GDT
var t = gdt.getTime().getByFormat('HH:mm:ss'); //Gets the time of GDT
gs.info(d); //2025-12-12
gs.info(t); //08:00:00
gs.info(gdt.getDate().getByFormat('EEEE')); //Friday
gs.info(gdt.getDate().getByFormat('E')); //Fri
gs.info(gdt.getDate().getByFormat('MMMM')); //December
gs.info(gdt.getDate().getByFormat('MMM')); //Dec
📤 Separando Data e Hora (displayValueInternal) - (old)
var gdt = new GlideDateTime();
var internalValue = gdt.getDisplayValueInternal(); // Ex: 2019-01-09 15:12:23
var parts = internalValue.split(' ');
gs.print('Data: ' + parts[0]); // 2019-01-09
gs.print('Hora: ' + parts[1]); // 15:12:23
for (var i = 0; i < arrDate.length; i++) {
gs.print(arrDate[i]);
}
//2019-01-09
//15:12:23
🔠 Função para obter o nome do mês (old)
//var monthNames = ',Janeiro,Fevereiro,Março,Abril,Maio,Junho,Julho,Agosto,Setembro,Outubro,Novembro,Dezembro'.split(',');
function getMonthName(month) {
var monthNames = ['', 'January','February','March','April','May','June','July','August','September','October','November','December'];
// Se nenhum parâmetro for passado, pega do GlideDateTime atual
if (!month || month <= 0 || month > 12) {
var gdt = new GlideDateTime();
month = gdt.getMonthLocalTime(); // Retorna 1-12
}
return monthNames[month];
}
- Script Summary
- Scripts - Oficial Doc.
- KB0594666 Do not use gs.nowDateTime() or GlideDateTime.getDisplayValue() in a GlideDateTime constructor.
Was useful, please leave your feedback!
- 5,007 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Good knowledge sharing, Thanks.
This below snippet is way simpler?
var month_name = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
var gdt = new GlideDateTime(); // >> takes current month of when code runs
var month_number = gdt.getMonth();
var curr_month = month_name[month_number-1];
gs.info(curr_month);
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Even though this thread is old it could still use a revisit.
The splitting of date / time, creation of arrays, methods etc is unnecessary as GDT / GlideDate and GlideTime already supports this.
var gdt = new GlideDateTime('2025-12-12 08:00:00');
var d = gdt.getDate().getValue(); //Gets the date of the GDT
var t = gdt.getTime().getByFormat('HH:mm:ss'); //Gets the time of GDT
gs.info(d); //2025-12-12
gs.info(t); //08:00:00
gs.info(gdt.getDate().getByFormat('EEEE')); //Friday
gs.info(gdt.getDate().getByFormat('E')); //Fri
gs.info(gdt.getDate().getByFormat('MMMM')); //December
gs.info(gdt.getDate().getByFormat('MMM')); //Dec