Skip to content

Instantly share code, notes, and snippets.

@Ashkanph
Created April 19, 2017 13:54
Show Gist options
  • Save Ashkanph/e9445cc2b8f7e34f103c71ee50e7deb0 to your computer and use it in GitHub Desktop.
Save Ashkanph/e9445cc2b8f7e34f103c71ee50e7deb0 to your computer and use it in GitHub Desktop.
Get the current datetime with javascript
/*
* Add this for jalali calendar:
* <script src="http://rawgithub.com/babakhani/PersianDate/master/dist/0.1.8/persian-date-0.1.8.min.js">
*
*/
var dateSeperatorCharacter = '-';
function currentDateTime(){
var cdt = {};
var currentDatetime = new Date();
// For current gregorian datetime
cdt.gNow = currentDatetime.getFullYear()+dateSeperatorCharacter+
( currentDatetime.getMonth()+1 <10 ? '0'+(currentDatetime.getMonth()+1):currentDatetime.getMonth()+1 ) +
dateSeperatorCharacter+(currentDatetime.getDate()<10 ? '0'+currentDatetime.getDate():currentDatetime.getDate())+"&nbsp;&nbsp;&nbsp;"+
(currentDatetime.getHours()<10 ? '0'+currentDatetime.getHours():currentDatetime.getHours()) + ":"+
(currentDatetime.getMinutes()<10 ? '0'+currentDatetime.getMinutes():currentDatetime.getMinutes())+":"+
(currentDatetime.getSeconds()<10 ? '0'+currentDatetime.getSeconds():currentDatetime.getSeconds());
// For current jalali datetime
currentDatetime =persianDate(currentDatetime);
cdt.jNow = currentDatetime.pDate.year+dateSeperatorCharacter+(currentDatetime.pDate.month<10 ? '0'+currentDatetime.pDate.month:currentDatetime.pDate.month)+dateSeperatorCharacter+(currentDatetime.pDate.date<10 ? '0'+currentDatetime.pDate.date:currentDatetime.pDate.date)+
"&nbsp;&nbsp;&nbsp;"+(currentDatetime.pDate.hours<10 ? '0'+currentDatetime.pDate.hours:currentDatetime.pDate.hours)+":"+
currentDatetime.pDate.minutes+":"+
// (currentDatetime.pDate.minutes<10 ? '0'+currentDatetime.pDate.minutes:currentDatetime.pDate.minutes)+"-"+
(currentDatetime.pDate.seconds<10 ? '0'+currentDatetime.pDate.seconds:currentDatetime.pDate.seconds);
return cdt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment