I have a date in format YYYYMMDDTHHMMSS and need to get the date in format DD.MM.YYYY HH:MM:SS .
I do such:
var dateTimeFormat;
var dateAsString = dataTimeFormat.split('', dateTimeFormat.lenght);
var year = dateAsString.splice(0, 4).join('');
var month = dateAsString.splice(0, 2).join('');
var day = dateAsString.splice(0, 2).join('');
var hours = dateAsString.splice(1, 2).join('');
var minutes = dateAsString.splice(1, 2).join('');
var seconds = dateAsString.splice(1, 2).join('');
var date = day + '.' + month + '.' + year + ' ' + hours + ':' + minutes + ':' + seconds;
return date;
But how can I convert date to Date format?
After transforming string into a known format, Date::parse() will be enough:
var yourDate = new Date(Date.parse(date));
WORKING DEMO:
var date = "2016.06.15 10:10:10";
var yourDate = new Date(Date.parse(date));
alert(yourDate);
NOTE: your format is a bit weird, but maybe parse will accept it as long as it accept many string formats such as:
Date.parse("Aug 9, 1995");
Date.parse("Wed, 09 Aug 1995 00:00:00");
Date.parse("Wed, 09 Aug 1995 00:00:00 GMT");
i think this is easy way to do this =). hope it help
<script>
dateTimeFormat = '20161506T112130';
str = dateTimeFormat.split("");
date = str[0] + str[1] + str[2] + str[3] + '.' + str[4] + str[5] + '.' + str[6] + str[7] + ' ' + str[9] + str[10] + ':' + str[11] + str[12] + ':' + str[13] + str[14];
console.log(date);
</script>
Related
i have a value of "08-28-2021 1:00:00 pm" is in $('#strDinnerStartTime').val)
i need to format it "08-28-2021 01:00:00 pm" where the hour is not just 1 but 01.
how can i do this thank you
The format that your date/time string is in is not recognized by javascript's built-in Date.parse() function. So, you may need to fallback to 'disassembling' the date/time string, formatting the hours with leading-zero padding, then 'reassembling' the date/time string, like so:
var s = "08-28-2021 1:00:00 pm";
var datepart = s.split(' ')[0];
var timepart1 = s.split(' ')[1];
var timepart2 = s.split(' ')[2];
var hours = timepart1.split(':')[0];
var minutes = timepart1.split(':')[1];
var seconds = timepart1.split(':')[2];
var hoursstr = String(hours).padStart(2, '0');
var minutesstr = String(minutes).padStart(2, '0');
var secondsstr = String(seconds).padStart(2, '0');
var result = datepart + ' ' + hoursstr + ':' + minutesstr + ':' + secondsstr + ' ' + timepart2;
console.log(result); //produces 08-28-2021 01:00:00 pm
I am trying to split a date using the following piece of JavaScript
var dSplit = getDate.split("/");
var newDate = dSplit[2] + "-" + dSplit[0] + "-" + dSplit[1];
I get the following output
2014 12:00:00 AM-11-25
The output i require is
2014-11-25 12:00:00 AM
Please Help.
One possible approach:
var getDate = '11/25/2014 12:00:00 AM';
var newDate = getDate.replace(/^\S+/, function(date) {
var d = date.split('/');
return d[2] + '-' + d[0] + '-' + d[1];
});
// 2014-11-25 12:00:00 AM
This approach allows to process both datetime strings (similar to '11/25/2014 12:00:00 AM', like in your answer) and date strings (like '11/25/2014'). The key here is processing only first sequence of non-whitespace characters in the string.
You may format the date to string as you want, using the next function:
function formatDate(date) {
var ans = date.getFullYear();
ans += "-" + (date.getMonth()+1);
ans += "-" + date.getDay();
ans += " " + date.getHours();
ans += ":" + date.getMinutes();
document.write (ans);
}
This way, even if the user's browser converts date to string on different order (longer format etc.) you have full control on the output string.
This may be helpfull,pass$val alone in function
var dateString=$val.split(" ");
var dateformat=dateString[0].split("-");
var dateVal= dateformat[0] + "/" + dateformat[1] + "/" + dateformat[2];
$.date = function(dateObject) {
var d = new Date(dateObject);
var day = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
if (day < 10) {
day = "0" + day;
}
if (month < 10) {
month = "0" + month;
}
var date = year + "-" + month + "-" + day;
return date;
};
how can I get the date in this format [mm/dd/yy] using javascript. I am struggling to get the 'year' to a 2 digit figure as opposed to the full 4 digits. Thanks!
var date = new Date();
var datestring = ("0" + (date.getMonth() + 1).toString()).substr(-2) + "/" + ("0" + date.getDate().toString()).substr(-2) + "/" + (date.getFullYear().toString()).substr(2);
This guarantees 2 digit dates and months.
Try this:
HTML
<div id="output"></div>
JS
(function () {
// Get current date
var date = new Date();
// Format day/month/year to two digits
var formattedDate = ('0' + date.getDate()).slice(-2);
var formattedMonth = ('0' + (date.getMonth() + 1)).slice(-2);
var formattedYear = date.getFullYear().toString().substr(2,2);
// Combine and format date string
var dateString = formattedMonth + '/' + formattedDate + '/' + formattedYear;
// Reference output DIV
var output = document.querySelector('#output');
// Output dateString
output.innerHTML = dateString;
})();
Fiddle: http://jsfiddle.net/kboucher/4mLe1Lrd/
How About this for the year
String(new Date().getFullYear()).substr(2)
And since you need your Month from 01 through 12 do this
var d = new Date("2013/8/3");
(d.getMonth() < 10 ? "0" : "") + (d.getMonth() + 1)
Do the same thing for days, Minutes and seconds
Working Demo
I'm working with Javascript dates, and I'm getting a bit confused with trying to take a date from a string.
This is the code I have:
var formatDate = function(dateObj) {
// make sure date values are two digits and months start at 1
var adjMonth = dateObj.getMonth() + 1;
var adjDate = dateObj.getDate();
if (adjMonth < 10) adjMonth = '0' + adjMonth;
if (adjDate < 10) adjDate = '0' + adjDate;
// build and return dateStr
var dateStr = dateObj.getFullYear() + '-' + adjMonth + '-' + adjDate;
return dateStr;
};
$(document).ready(function() {
var testIn1 = "2012-02-01";
var testDate1 = new Date(testIn1);
var testDate1Str = formatDate(testDate1);
var testIn2 = "2012-01-31";
var testDate2 = new Date(testIn2);
var testDate2Str = formatDate(testDate2);
$('#output').html("---Input = '" + testIn1 + "':<br>" + testDate1 + "<br>" + testDate1Str + "<br>"
+"---Input = '" + testIn2 + "':<br>" + testDate2 + "<br>" + testDate2Str + "<br>");
});
Results I get from this are:
---Input = '2012-02-01':
Tue Jan 31 2012 18:00:00 GMT-0600 (CST)
2012-01-31
---Input = '2012-01-31':
Mon Jan 30 2012 18:00:00 GMT-0600 (CST)
2012-01-30
Which makes no sense to me, why are the days one off? Doesn't seem sensical to get 2012-01-31 from 2012-02-01... What am I missing here?
It looks like Date.parse uses 00:00:00 GMT if you don't pass a time, and it will be 18:00:00 the previous day in your time zone (GMT-6). If you do pass an explicit time, then this behaviour is suppressed:
Date.parse(testIn1 + " 00:00:00");
I need to get a date in this form:
"12/31/2020 5:00 AM"
I wrote this code:
var ExpireI = new Date ();
ExpireI.setTime(ExpireI.getTime() + (24 * 3600 * 1000));
alert(ExpireI.toGMTString());
but it display something like this:
Tue, 26 Jun 2012 10:26:35 GMT
so, how can I get a date in that form ?
Try this
Instead of doing this way:
getDate() // Returns the date
getMonth() // Returns the month
getFullYear() // Returns the year
And for your case, you can use this way:
alert(ExpireI.getFullYear() + "-" + ExpireI.getMonth() + "-" + ExpireI.getDate());
Fiddle here... :)
This should work,
var d = new Date ();
var date = d.getDate();
var month = d.getMonth() + 1; // because january is 0 in javascript
var year = d.getFullYear();
var hour = d.getHours();
var min = d.getMinutes();
var m;
if(hour > 12 ){
hour-=12;
m = 'PM';
}
else{
m = 'AM';
}
console.log(month + '/' + date + '/' + year + ' ' + hour + ':' + min + ' ' + m);