Created
January 3, 2017 10:02
-
-
Save x9t9/7135c16e6dda842aa03cab5bfe9670e9 to your computer and use it in GitHub Desktop.
js Date and Time object examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get current date time | |
var currentDate = new Date(); | |
// get date | |
var date = currentDate.getDate(); | |
// get month index (January is 0) | |
var month = currentDate.getMonth(); | |
// get year | |
var year = currentDate.getFullYear(); | |
// get time | |
var timestamp = currentDate.getTime(); | |
// date string | |
var dateString = date + "-" +(month + 1) + "-" + year; | |
// Create by saperate argument year, month, day, hour, minute, second | |
var date = new Date(2016, 6, 27, 13, 30, 0); | |
// Parse date time | |
var date = new Date("Wed, 27 July 2016 13:30:00"); | |
var date = new Date("Wed, 27 July 2016 07:45:00 GMT"); | |
var date = new Date("27 July 2016 13:30:00 GMT+05:45"); | |
// Parse with ISO format | |
var date = new Date("2016-07-27T07:45:00Z"); | |
// Without time zone with will set timezone to local time zone | |
// 25 July 2016 00:00:00 local time zone | |
var date = new Date("25 July 2016") | |
var date = new Date("July 25, 2016") | |
// Format | |
// Pad | |
function pad(n) { | |
return n<10 ? '0'+n : n; | |
} | |
// 01-03-2017 | |
var mmddyyyy = pad(month + 1) + "-" + pad(date) + "-" + year; | |
// month in human | |
var monthNames = [ | |
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" | |
]; | |
// January 03, 2017 | |
var dateWithFullMonthName = monthNames[month] + " " + pad(date) + ", " + year; | |
// ordinal | |
function ordinal(n) { | |
var s=["th","st","nd","rd"], | |
v=n%100; | |
return n+(s[(v-20)%10]||s[v]||s[0]); | |
} | |
// 03 January, 2017 | |
var ordinalDate = ordinal(date) + " " + monthNames[month] + ", " + year; | |
// day of week | |
var daysOfWeek = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; | |
// Tue, 03 January, 2017 | |
var ordinalDateWithDayOfWeek = daysOfWeek[currentDate.getDay()] + ", " + ordinalDate; | |
// toLocalDateString | |
new Date().toLocaleDateString() | |
// 03 Jan 2017 | |
var today = new Date().toLocaleDateString('en-GB', { | |
day : 'numeric', | |
month : 'short', | |
year : 'numeric' | |
}) | |
// 1/3/2017 | |
var today = new Date().toLocaleDateString(undefined, { | |
day:'numeric', | |
month: 'numeric', | |
year: 'numeric' | |
}) | |
// 01/03/2017 | |
var today = new Date().toLocaleDateString(undefined, { | |
day: '2-digit', | |
month: '2-digit', | |
year: 'numeric' | |
}) | |
// add 2 days to date | |
var date = new Date(); | |
var nextDate = date.getDate() + 2; | |
date.setDate(nextDate); | |
var newDate = date.toLocaleString(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment