Desi Delite
Desi Delite

Reputation: 191

How to convert UTC date time into yyyy-mm-dd hh:mm:ss date time format using javaScript?

This is very simple question.There is a description inside a div tag and it contain a date time(format is yyyy-mm-dd hh:mm:ss). I catched this date time using regular expression and then convert into date.

Suppose I have "Now 2015-01-02 11:47:50 some statements.....".So after all, I wished to get "2015-01-02 11:47:50" part only. But Now I received it as "Fri Jan 02 2015 03:47:50 GMT-0800 (Pacific Standard Time)".

Where was I wrong? Can anyone please give me a advice to get the result as yyyy-mm-dd hh:mm:ss format?

Thank you..

(Here I attached the code I tried)

var getDiv = document.getElementById('aasd');
var aa = getDiv.innerHTML;
var result = dateCatcher(aa);  
alert(dateCatcher(aa));    
    
//Gives the date part from the whole statement    
function dateCatcher(statement){

    var date_finder =/(\d{4})\-(\d{2})\-(\d{2})\s+(\d{2}):(\d{2}):(\d{2})/;
    var datePart = statement.match(date_finder);
    datePart[2] -= 1;
    var UtcDate = new Date(Date.UTC.apply(this, datePart.slice(1)));
    return UtcDate;
}     
<html>
<body>
<div id = "aasd"> Hi, Now 2015-01-02 11:47:50 ddfd dfdsfdf dff </div>    

</body>

</html>

Upvotes: 0

Views: 7909

Answers (3)

Oscar Zhang
Oscar Zhang

Reputation: 380

The following code will do the thing, plus it will add and "0" if dd and mm < 10.

function getUTCTime(d){
    utcTime = new Date(d)
    var dd = utcTime.getDate(); 
    var mm = utcTime.getMonth()+1;
    var yyyy = utcTime.getFullYear(); 
    var hh = utcTime.getHours(); 
    var min = utcTime.getMinutes(); 
    var ss = utcTime.getSeconds(); 
    if(dd<10){dd='0'+dd} 
    if(mm<10){mm='0'+mm};
    return yyyy + "-" + mm + "-" + dd + " " + hh + ":" + min + ":" + ss;
}

Take a look at JavaScript Date Reference:

Upvotes: 1

Vasil Indzhev
Vasil Indzhev

Reputation: 695

I would suggest this as a solution:

var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1; //months from 1-12
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
var hours = dateObj.getUTCHours();
var minutes = dateObj.getUTCMinutes();
var seconds = dateObj.getUTCSeconds();
        
var newdate = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;

$('#time').append('Time is: ' + newdate);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="time"></p>

You can check this too. It might be helpful: Complete list of date object functions: DATE

getMonth()

Returns the month (0-11) in the specified date according to local time.

getUTCMonth()

Returns the month (0-11) in the specified date according to universal time.

Upvotes: 1

Vishnu Katpure
Vishnu Katpure

Reputation: 395

try this use dateformatter.js as given below function

Date.prototype.customFormat = function(formatString){
    var YYYY,YY,MMMM,MMM,MM,M,DDDD,DDD,DD,D,hhh,hh,h,mm,m,ss,s,ampm,AMPM,dMod,th;
    var dateObject = this;
    YY = ((YYYY=dateObject.getFullYear())+"").slice(-2);
    MM = (M=dateObject.getMonth()+1)<10?('0'+M):M;
    MMM = (MMMM=["January","February","March","April","May","June","July","August","September","October","November","December"][M-1]).substring(0,3);
    DD = (D=dateObject.getDate())<10?('0'+D):D;
    DDD = (DDDD=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][dateObject.getDay()]).substring(0,3);
    th=(D>=10&&D<=20)?'th':((dMod=D%10)==1)?'st':(dMod==2)?'nd':(dMod==3)?'rd':'th';
    formatString = formatString.replace("#YYYY#",YYYY).replace("#YY#",YY).replace("#MMMM#",MMMM).replace("#MMM#",MMM).replace("#MM#",MM).replace("#M#",M).replace("#DDDD#",DDDD).replace("#DDD#",DDD).replace("#DD#",DD).replace("#D#",D).replace("#th#",th);

    h=(hhh=dateObject.getHours());
    if (h==0) h=24;
    if (h>12) h-=12;
    hh = h<10?('0'+h):h;
    AMPM=(ampm=hhh<12?'am':'pm').toUpperCase();
    mm=(m=dateObject.getMinutes())<10?('0'+m):m;
    ss=(s=dateObject.getSeconds())<10?('0'+s):s;
    return formatString.replace("#hhh#",hhh).replace("#hh#",hh).replace("#h#",h).replace("#mm#",mm).replace("#m#",m).replace("#ss#",ss).replace("#s#",s).replace("#ampm#",ampm).replace("#AMPM#",AMPM);
}
function formatInputDate(inputDate, dateFormat) {
    if(inputDate) { 
        var dateToFormat = new Date(inputDate);
        return dateToFormat.customFormat(dateFormat);
    }
}

and call like

console.log(formatInputDate(yourDate,'#YYYY #MMM# #DD# '));

Upvotes: 0

Related Questions