-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuncoes_datas.html
More file actions
83 lines (62 loc) · 2.35 KB
/
funcoes_datas.html
File metadata and controls
83 lines (62 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript - JS Date</title>
<script>
/*
//Date é um objeto que precisa ser instânciado
var data = new Date()
//Recuperando Dia Mês e Ano //getMonth -> 0 a 11
//document.write(data.getDate() + '/' + (data.getMonth() + 1) + '/' + data.getFullYear())
//-> adicionar / remover dias
document.write(data.toString())
data.setDate(data.getDate() + 720)
document.write('<hr />')
document.write(data.toString())
document.write('<br /> <br /> <hr />')
//-> adicionar / remover meses
document.write(data.toString())
data.setMonth(data.getMonth() -1)
document.write('<hr />')
document.write(data.toString())
document.write('<br /> <br /> <hr />')
//-> adicionar / remover anos
document.write(data.toString())
data.setFullYear(data.getFullYear() -1)
document.write('<hr />')
document.write(data.toString())
document.write('<br /> <br /> <hr />')
*/
//new Date(year,month,day,hours,minutes,seconds,ms) -> paramêtros do objeto Date
// 15/01/2017
var data1 = new Date(2017, 0, 15)
// 23/02/2017
var data2 = new Date(2017, 1, 23)
document.write(data1.toString())
document.write('<hr />')
document.write(data2.toString())
document.write('<br /> <br /> <hr />')
//converter datas para algo que possamos calcular
document.write(data1.getTime())
document.write('<hr />')
document.write(data2.getTime())
//getTime() -> recupera os ms entre (1 de janeiro de 1970) até a data em questão
document.write('<br /> <br /> <hr />')
//encontrar a quantidade de milissegundos entre data1 e data2
var milissegundos_entre_datas = Math.abs(data1.getTime() - data2.getTime())
document.write(milissegundos_entre_datas)
//1 dia tem 24h
//cada hora tem 60min
//cada min tem 60 segundos
//cada segundo tem 1000 milissegundos
//então quantos milissegundos existem em um dia?
var milissegundos_por_dia = (1*24*60*60*1000)
document.write(' 1 dia tem: ' + milissegundos_por_dia + ' milissegundos')
document.write('<br /> <br /> <hr />')
document.write('A diferença entre data1 e data2 é de ' + Math.ceil(milissegundos_entre_datas / milissegundos_por_dia) + ' dias')
</script>
</head>
<body>
</body>
</html>