This blog will show you how to customize date and time through javascript in different formats.
To check any condition, I have used javascript ternary operator instead of IF statement that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is true followed by a colon ( : ), and finally the expression to execute if the condition is false.
To Get Different Javascript Date Format(like dd/mm/yyyy or yyyy-mm-dd and so on) From Current Date
Just edit line number 13 in the below code and get any JS date format.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<script> var today = new Date();//getting today's date and time //Setting Up Date format (yyyy/mm/dd) var dd = today.getDate();//getting date part from today's date. var yyyy = today.getFullYear();//getting year from today's date. var mm = today.getMonth()+1 ;// getting month from today's date. mm = (mm<10?'0':'')+mm; // if month is less than 10 then append '0' before it to make it 'mm' format. dd = (dd<10?'0':'') + dd;// if date is less than 10 then append '0' before it to make it 'dd' format. document.write(yyyy+"-"+mm+"-"+dd); </script> |
To Extract Time From Current Date Time and Format As hh:mm:ss
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<script> var today = new Date();//getting today's date and time // Now setting up Time Format (hh:mm:ss) var hr = today.getHours();//getting hour part from today's date. var min = today.getMinutes();//getting minutes part from today's date. var sec = today.getSeconds();//getting seconds part from today's date. hr = (hr<10?'0':'') + hr;// if hr is less than 10 then append '0' before it to make 'hh' format min = (min<10?'0':'') + min;// if min is less than 10 then append '0' before it to make 'mm' format sec=(sec<10?'0':'') + sec;// if sec is less than 10 then append '0' before it to make 'ss' format document.write(hr+":"+min+":"+sec); </script> |
To Get Date and Time Both
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<script> var today = new Date();//getting today's date and time //Setting Up Date format (yyyy/mm/dd) var dd = today.getDate();//getting date part from today's date. var yyyy = today.getFullYear();//getting year from today's date. var mm = today.getMonth()+1 ;// getting month from today's date. mm = (mm<10?'0':'')+mm; // if month is less than 10 then append '0' before it to make it 'mm' format. dd = (dd<10?'0':'') + dd;// if date is less than 10 then append '0' before it to make it 'dd' format. // Now setting up Time Format (hh:mm:ss) var hr = today.getHours();//getting hour part from today's date. var min = today.getMinutes();//getting minutes part from today's date. var sec = today.getSeconds();//getting seconds part from today's date. hr = (hr<10?'0':'') + hr;// if hr is less than 10 then append '0' before it to make 'hh' format min = (min<10?'0':'') + min;// if min is less than 10 then append '0' before it to make 'mm' format sec=(sec<10?'0':'') + sec;// if sec is less than 10 then append '0' before it to make 'ss' format document.write(yyyy+"-"+mm+"-"+dd+" "+hr+":"+min+":"+sec+"\n"); </script> |
Customize date format using Moment Js
If you want to customize the current date format then it is very easy with the moment.js. You need to download and import the moment.js and then use the predefined functions provided by the package.
To customize current date use below
1 |
moment().format('YYYY-MM-DD') |
To add or subtract days from current date use below
1 |
moment().subtract(1, 'days').format('YYYY-MM-DD') |
Customize date format using toLocaleDateString(locales)
We can use different timezones to get the different date formates using toLocaleDateString().
For example if we need YYYY-MM-DD format then we can use “en-CA”.
1 |
new Date().toLocaleDateString("en-CA") |
Below is the list of other options you can use
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
ar: dd/MM/yy bg: d.M.yyyy 'г.' ca: dd/MM/yyyy zh-Hans: yyyy/M/d cs: d.M.yyyy da: dd-MM-yyyy de: dd.MM.yyyy el: d/M/yyyy en: M/d/yyyy es: dd/MM/yyyy fi: d.M.yyyy fr: dd/MM/yyyy he: dd/MM/yyyy hu: yyyy.MM.dd. is: d.M.yyyy it: dd/MM/yyyy ja: yyyy/MM/dd ko: yyyy-MM-dd nl: d-M-yyyy no: dd.MM.yyyy pl: yyyy-MM-dd pt: dd/MM/yyyy rm: dd/MM/yyyy ro: dd.MM.yyyy ru: dd.MM.yyyy hr: d.M.yyyy. sk: d. M. yyyy sq: yyyy-MM-dd sv: yyyy-MM-dd th: d/M/yyyy tr: dd.MM.yyyy ur: dd/MM/yyyy id: dd/MM/yyyy uk: dd.MM.yyyy be: dd.MM.yyyy sl: d.M.yyyy et: d.MM.yyyy lv: yyyy.MM.dd. lt: yyyy.MM.dd tg: dd.MM.yy fa: MM/dd/yyyy vi: dd/MM/yyyy hy: dd.MM.yyyy az: dd.MM.yyyy eu: yyyy/MM/dd hsb: d. M. yyyy mk: dd.MM.yyyy tn: yyyy/MM/dd xh: yyyy/MM/dd zu: yyyy/MM/dd af: yyyy/MM/dd ka: dd.MM.yyyy fo: dd-MM-yyyy hi: dd-MM-yyyy mt: dd/MM/yyyy se: dd.MM.yyyy ga: dd/MM/yyyy ms: dd/MM/yyyy kk: dd.MM.yyyy ky: dd.MM.yy sw: M/d/yyyy tk: dd.MM.yy uz: dd/MM yyyy tt: dd.MM.yyyy bn: dd-MM-yy pa: dd-MM-yy gu: dd-MM-yy or: dd-MM-yy ta: dd-MM-yyyy te: dd-MM-yy kn: dd-MM-yy ml: dd-MM-yy as: dd-MM-yyyy mr: dd-MM-yyyy sa: dd-MM-yyyy mn: yy.MM.dd bo: yyyy/M/d cy: dd/MM/yyyy km: yyyy-MM-dd lo: dd/MM/yyyy gl: dd/MM/yyyy kok: dd-MM-yyyy syr: dd/MM/yyyy si: yyyy-MM-dd iu: d/MM/yyyy am: d/M/yyyy tzm: dd-MM-yyyy ne: M/d/yyyy fy: d-M-yyyy ps: dd/MM/yy fil: M/d/yyyy dv: dd/MM/yy ha: d/M/yyyy yo: d/M/yyyy quz: dd/MM/yyyy nso: yyyy/MM/dd ba: dd.MM.yy lb: dd/MM/yyyy kl: dd-MM-yyyy ig: d/M/yyyy ii: yyyy/M/d arn: dd-MM-yyyy moh: M/d/yyyy br: dd/MM/yyyy : MM/dd/yyyy ug: yyyy-M-d mi: dd/MM/yyyy oc: dd/MM/yyyy co: dd/MM/yyyy gsw: dd/MM/yyyy sah: MM.dd.yyyy qut: dd/MM/yyyy rw: M/d/yyyy wo: dd/MM/yyyy prs: dd/MM/yy gd: dd/MM/yyyy ar-SA: dd/MM/yy bg-BG: d.M.yyyy 'г.' ca-ES: dd/MM/yyyy zh-TW: yyyy/M/d cs-CZ: d.M.yyyy da-DK: dd-MM-yyyy de-DE: dd.MM.yyyy el-GR: d/M/yyyy en-US: M/d/yyyy fi-FI: d.M.yyyy fr-FR: dd/MM/yyyy he-IL: dd/MM/yyyy hu-HU: yyyy.MM.dd. is-IS: d.M.yyyy it-IT: dd/MM/yyyy ja-JP: yyyy/MM/dd ko-KR: yyyy-MM-dd nl-NL: d-M-yyyy nb-NO: dd.MM.yyyy pl-PL: yyyy-MM-dd pt-BR: dd/MM/yyyy rm-CH: dd/MM/yyyy ro-RO: dd.MM.yyyy ru-RU: dd.MM.yyyy hr-HR: d.M.yyyy. sk-SK: d. M. yyyy sq-AL: yyyy-MM-dd sv-SE: yyyy-MM-dd th-TH: d/M/yyyy tr-TR: dd.MM.yyyy ur-PK: dd/MM/yyyy id-ID: dd/MM/yyyy uk-UA: dd.MM.yyyy be-BY: dd.MM.yyyy sl-SI: d.M.yyyy et-EE: d.MM.yyyy lv-LV: yyyy.MM.dd. lt-LT: yyyy.MM.dd tg-Cyrl-TJ: dd.MM.yy fa-IR: MM/dd/yyyy vi-VN: dd/MM/yyyy hy-AM: dd.MM.yyyy az-Latn-AZ: dd.MM.yyyy eu-ES: yyyy/MM/dd hsb-DE: d. M. yyyy mk-MK: dd.MM.yyyy tn-ZA: yyyy/MM/dd xh-ZA: yyyy/MM/dd zu-ZA: yyyy/MM/dd af-ZA: yyyy/MM/dd ka-GE: dd.MM.yyyy fo-FO: dd-MM-yyyy hi-IN: dd-MM-yyyy mt-MT: dd/MM/yyyy se-NO: dd.MM.yyyy ms-MY: dd/MM/yyyy kk-KZ: dd.MM.yyyy ky-KG: dd.MM.yy sw-KE: M/d/yyyy tk-TM: dd.MM.yy uz-Latn-UZ: dd/MM yyyy tt-RU: dd.MM.yyyy bn-IN: dd-MM-yy pa-IN: dd-MM-yy gu-IN: dd-MM-yy or-IN: dd-MM-yy ta-IN: dd-MM-yyyy te-IN: dd-MM-yy kn-IN: dd-MM-yy ml-IN: dd-MM-yy as-IN: dd-MM-yyyy mr-IN: dd-MM-yyyy sa-IN: dd-MM-yyyy mn-MN: yy.MM.dd bo-CN: yyyy/M/d cy-GB: dd/MM/yyyy km-KH: yyyy-MM-dd lo-LA: dd/MM/yyyy gl-ES: dd/MM/yyyy kok-IN: dd-MM-yyyy syr-SY: dd/MM/yyyy si-LK: yyyy-MM-dd iu-Cans-CA: d/M/yyyy am-ET: d/M/yyyy ne-NP: M/d/yyyy fy-NL: d-M-yyyy ps-AF: dd/MM/yy fil-PH: M/d/yyyy dv-MV: dd/MM/yy ha-Latn-NG: d/M/yyyy yo-NG: d/M/yyyy quz-BO: dd/MM/yyyy nso-ZA: yyyy/MM/dd ba-RU: dd.MM.yy lb-LU: dd/MM/yyyy kl-GL: dd-MM-yyyy ig-NG: d/M/yyyy ii-CN: yyyy/M/d arn-CL: dd-MM-yyyy moh-CA: M/d/yyyy br-FR: dd/MM/yyyy ug-CN: yyyy-M-d mi-NZ: dd/MM/yyyy oc-FR: dd/MM/yyyy co-FR: dd/MM/yyyy gsw-FR: dd/MM/yyyy sah-RU: MM.dd.yyyy qut-GT: dd/MM/yyyy rw-RW: M/d/yyyy wo-SN: dd/MM/yyyy prs-AF: dd/MM/yy gd-GB: dd/MM/yyyy ar-IQ: dd/MM/yyyy zh-CN: yyyy/M/d de-CH: dd.MM.yyyy en-GB: dd/MM/yyyy es-MX: dd/MM/yyyy fr-BE: d/MM/yyyy it-CH: dd.MM.yyyy nl-BE: d/MM/yyyy nn-NO: dd.MM.yyyy pt-PT: dd-MM-yyyy sr-Latn-CS: d.M.yyyy sv-FI: d.M.yyyy az-Cyrl-AZ: dd.MM.yyyy dsb-DE: d. M. yyyy se-SE: yyyy-MM-dd ga-IE: dd/MM/yyyy ms-BN: dd/MM/yyyy uz-Cyrl-UZ: dd.MM.yyyy bn-BD: dd-MM-yy mn-Mong-CN: yyyy/M/d iu-Latn-CA: d/MM/yyyy tzm-Latn-DZ: dd-MM-yyyy quz-EC: dd/MM/yyyy ar-EG: dd/MM/yyyy zh-HK: d/M/yyyy de-AT: dd.MM.yyyy en-AU: d/MM/yyyy es-ES: dd/MM/yyyy fr-CA: yyyy-MM-dd sr-Cyrl-CS: d.M.yyyy se-FI: d.M.yyyy quz-PE: dd/MM/yyyy ar-LY: dd/MM/yyyy zh-SG: d/M/yyyy de-LU: dd.MM.yyyy en-CA: dd/MM/yyyy es-GT: dd/MM/yyyy fr-CH: dd.MM.yyyy hr-BA: d.M.yyyy. smj-NO: dd.MM.yyyy ar-DZ: dd-MM-yyyy zh-MO: d/M/yyyy de-LI: dd.MM.yyyy en-NZ: d/MM/yyyy es-CR: dd/MM/yyyy fr-LU: dd/MM/yyyy bs-Latn-BA: d.M.yyyy smj-SE: yyyy-MM-dd ar-MA: dd-MM-yyyy en-IE: dd/MM/yyyy es-PA: MM/dd/yyyy fr-MC: dd/MM/yyyy sr-Latn-BA: d.M.yyyy sma-NO: dd.MM.yyyy ar-TN: dd-MM-yyyy en-ZA: yyyy/MM/dd es-DO: dd/MM/yyyy sr-Cyrl-BA: d.M.yyyy sma-SE: yyyy-MM-dd ar-OM: dd/MM/yyyy en-JM: dd/MM/yyyy es-VE: dd/MM/yyyy bs-Cyrl-BA: d.M.yyyy sms-FI: d.M.yyyy ar-YE: dd/MM/yyyy en-029: MM/dd/yyyy es-CO: dd/MM/yyyy sr-Latn-RS: d.M.yyyy smn-FI: d.M.yyyy ar-SY: dd/MM/yyyy en-BZ: dd/MM/yyyy es-PE: dd/MM/yyyy sr-Cyrl-RS: d.M.yyyy ar-JO: dd/MM/yyyy en-TT: dd/MM/yyyy es-AR: dd/MM/yyyy sr-Latn-ME: d.M.yyyy ar-LB: dd/MM/yyyy en-ZW: M/d/yyyy es-EC: dd/MM/yyyy sr-Cyrl-ME: d.M.yyyy ar-KW: dd/MM/yyyy en-PH: M/d/yyyy es-CL: dd-MM-yyyy ar-AE: dd/MM/yyyy es-UY: dd/MM/yyyy ar-BH: dd/MM/yyyy es-PY: dd/MM/yyyy ar-QA: dd/MM/yyyy en-IN: dd-MM-yyyy es-BO: dd/MM/yyyy en-MY: d/M/yyyy es-SV: dd/MM/yyyy en-SG: d/M/yyyy es-HN: dd/MM/yyyy es-NI: dd/MM/yyyy es-PR: dd/MM/yyyy es-US: M/d/yyyy bs-Cyrl: d.M.yyyy bs-Latn: d.M.yyyy sr-Cyrl: d.M.yyyy sr-Latn: d.M.yyyy smn: d.M.yyyy az-Cyrl: dd.MM.yyyy sms: d.M.yyyy zh: yyyy/M/d nn: dd.MM.yyyy bs: d.M.yyyy az-Latn: dd.MM.yyyy sma: yyyy-MM-dd uz-Cyrl: dd.MM.yyyy mn-Cyrl: yy.MM.dd iu-Cans: d/M/yyyy zh-Hant: d/M/yyyy nb: dd.MM.yyyy sr: d.M.yyyy tg-Cyrl: dd.MM.yy dsb: d. M. yyyy smj: yyyy-MM-dd uz-Latn: dd/MM yyyy mn-Mong: yyyy/M/d iu-Latn: d/MM/yyyy tzm-Latn: dd-MM-yyyy ha-Latn: d/M/yyyy zh-CHS: yyyy/M/d zh-CHT: d/M/yyyy* |