I am using a grid view which is getting bind by json also on some conditions and a coloum of grid contains date , so while getting data from json, I need to parse the date. I am able to get date but not time part . Tried and searched too much . I am mentioning below two methods that I tried but not solves my problem.
{
function ParseDate(jsonDate) {
date = new Date(parseInt(String(jsonDate).substr(6)));
day = date.getDate();
month = date.getMonth() + 1;
year = date.getFullYear();
return month + "/" + day + "/" + year;
}
}
This gives me only date but I need time, so I did one more method
{
function ParseDate(jsonDate) {
var date = new Date(parseInt(jsonDate.substr(6)));
var formatted = ("0" + (date.getMonth() + 1)).slice(-2) + "/" + ("0" + date.getDate()).slice(-2) + "/" + date.getFullYear() + " " + date.getHours() + ":" + date.getMinutes() + ":" + "0" + date.getSeconds();
return formatted;
}
}
but this function returns
//07/19/2013 11:38
instead of //7/19/2013 11:38:07 AM which is desired result.Please help me solving this problem. Thank You very much. Also , I need to show Am or PM that is compulsory
Try this:
function ParseDateToLocale(jsonDate) {
var date = new Date(parseInt(jsonDate.substr(6)));
var myDate = new Date(date);
var formatted = myDate.toLocaleString();
return formatted;
}
See it working here: http://jsfiddle.net/2ft3A/.
Try this one, this will help you,
function ParseDate(jsonDate) {
var date = new Date(parseInt(jsonDate.substr(6)));
var merd='';
if(date.getHours()>=12)
{
merd='PM';
}
else
{
merd='AM';
}
var formatted = ("0" + (date.getMonth() + 1)).slice(-2) + "/" + ("0" + date.getDate()).slice(-2) + "/" + date.getFullYear() + " " + date.getHours() + ":" + date.getMinutes() + ":" + "0" + date.getSeconds()+ " " + merd;
return formatted;
}
dateObject.toLocaleTimeString(); (OR) date.getHours() will return 0-24hrs. Base on that value you can append AM or PM
Related
This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 5 years ago.
I should format my datetime value in javascript to this format yyyy-MM-dd hh:mm:ss
I tried this
var btf = new Date(value.createDate);
var billingTimeFormatted = btf.getFullYear() + "-" + btf.getMonth() + "-" + btf.getDate() + " " + btf.getHours() + ":" + btf.getMinutes() + ":" + btf.getSeconds();
But it result to this
2017-8-31 02:00:00
month and date should be 2-digit (08 intead of 8)
What could be the best workaround?
*type on minutes is edited
Nothing wrong with your code. Javascript returns the integer < 10 in single digit only. Format it to string of 2 with a function.
function formatWithZero(val)
{
// as per comment by #RobG below, return ('0'+val).slice(-2) will also
// do the same thing in lesser lines of code. It works and can be used.
var value = val.toString();
if(value.length > 1)
return value;
var str = "00"+value;
return str.slice(str.length-2,str.length); ;
}
//I am using Date.now(), you can use your value.
var btf = new Date(Date.now());
var billingTimeFormatted = btf.getFullYear() + "-" + formatWithZero(btf.getMonth()) + "-" + formatWithZero(btf.getDate()) + " " + formatWithZero(btf.getHours()) + ":" + formatWithZero(btf.getMinutes()) + ":" + formatWithZero(btf.getSeconds());
alert(billingTimeFormatted);
You forgot () for getMinutes(), to have 2 digits :
var btf = new Date();
var currentMonth = btf.getMonth();
if (currentMonth < 10) { currentMonth = '0' + currentMonth; }
var billingTimeFormatted = btf.getFullYear() + "-" + currentMonth + "-" + btf.getDate() + " " + btf.getHours() + ":" + btf.getMinutes() + ":" + btf.getSeconds();
I have the following value:
javascript variable:
"2015-10-14T17:54:19.033"
I want to end up with
mm/dd/yyyy
e.g.
10/14/2015
I was trying to do
var date = month[d.getMonth()] + " " + d.getDay()+ ", " + d.getFullYear();
Here is my Fiddle
http://jsfiddle.net/bthorn/7ptcedt4/
var d = new Date('2015-10-14T17:54:19.033');
var date = d.getMonth().toString() + "/" + d.getDay().toString() + "/" + d.getFullYear().toString();
console.log(date);
Something is NOT right: I am getting
9/3/2015
You can use this:
var date = new Date("2015-10-14T17:54:19.033");
var formatedDateString = (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
Or use moment.js
var formatedDateString = moment("2015-10-14T17:54:19.033").format('MM/dd/YYYY');
Or may you consider create your own date format function.
PS.: javascript months are 0-indexed, so for the correct month you should use (date.getMonth() + 1)
I have a input date field whose value is in this "2014-06-07 07:14"(year-month-date hour:minute) format how can i change it to 06/07/2014 07/14( mm/dd/yy) format using jquery.
DateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("dd/MM/yyyt hh/mm");
Date date = originalFormat.parse("2014-06-07 07:14");
String formattedDate = targetFormat.format(date); // 06/07/2014 07/14
Docs of SimpleDateFormat#format
PS: A JavaScript implementation of the format() method of Java's SimpleDateFormat: is available here
Why using jQuery to do this ?
Use plain js:
var date = new Date('2014-06-07 07:14');
alert((date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear() + ' ' + date.getHours() + '/' + date.getMinutes());
http://jsfiddle.net/F4nq8/
It seems that IE and Safari have some bug with YYYYMMDD dates so a workaround could be something like:
var s = "2014-06-07 07:14";
var t = s.split(" ");
var d = t[0].split("-");
var x = d[1] + "/" + d[2] + "/" + d[0] + " " + t[1];
var date = new Date(x);
alert((date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear() + ' ' + date.getHours() + '/' + date.getMinutes());
http://jsfiddle.net/F4nq8/4/
Some reference about this behaviour: http://biostall.com/javascript-new-date-returning-nan-in-ie-or-invalid-date-in-safari
I have to send Date object for current date to my back end from javascript
What I am doing is
var currentDate = new Date();
var dateString = currentDate.getMonth() + "-"
+ currentDate.getDate() + "-" + currentDate.getFullYear() + " "
+ currentDate.getHours() + ":" + currentDate.getMinutes() + ":"
+ currentDate.getSeconds();
var newDate = new Date(Date.parse(dateString));
But it is saying Invalid Date to newDate.
I have to send 3-10-2013 6:10:25 PM as datetime object to backend.
var currentDate = new Date(),
utcYear = currentDate.getUTCFullYear(),
utcMonth = ('0' + currenctDate.getUTCMonth()).slice(-2),
utcDay = ('0' + currentDate.getUTCDate()).slice(-2),
fullDateString = utcMonth.toString() + '/' + utcDay.toString() + '/' + utcYear.toString();
Same principle if you want to get the time part as well.
Instead of putting - in between the month/date and date/year, just put spaces.
var currentDate = new Date(),
dateString = currentDate.getMonth() + " " +
currentDate.getDate() + " " +
currentDate.getFullYear() + " " +
currentDate.getHours() + ":" +
currentDate.getMinutes() + ":" +
currentDate.getSeconds(),
newDate = new Date(dateString);
console.log(newDate)
For using the Amazon mechanical turk API I want to get the current GMT time and show it in ISO format
2011-02-24T20:38:34Z
I am wondering if there is any way to correctly get the gmt time and also be able to reformat it with ISO format. I can use something like now.toGMTString(); but it makes a string out of the date and it is hard to reformat it with ISO.
var year = now.getUTCFullYear()
var month = now.getUTCMonth()
var day= now.getUTCDay()
var hour= now.getUTCHours()
var mins= now.getUTCMinutes()
var secs= now.getUTCSeconds()
var dateString = year + "-" + month + "-" + day + "T" + hour + ":" + mins + ":" + secs + "Z"
You should be using UTC now instead of GMT. (Amounts to almost the same thing now, and it is the new standard anyway)
I believe this will work for you:
Number.prototype.pad = function(width,chr){
chr = chr || '0';
var result = this;
for (var a = 0; a < width; a++)
result = chr + result;
return result.slice(-width);
}
Date.prototype.toISOString = function(){
return this.getUTCFullYear().pad(4) + '-'
+ this.getUTCMonth().pad(2) + '-'
+ this.getUTCDay().pad(2) + 'T'
+ this.getUTCHours().pad(2) + ':'
+ this.getUTCMinutes().pad(2) + ':'
+ this.getUTCSeconds().pad(2) + 'Z';
}
Usage:
var d = new Date;
alert('ISO Format: '+d.toISOString());
Not much more different than every else's answer, but make it built-in to the date object for convenience
function pad(num) {
return ("0" + num).slice(-2);
}
function formatDate(d) {
return [d.getUTCFullYear(),
pad(d.getUTCMonth() + 1),
pad(d.getUTCDate())].join("-") + "T" +
[pad(d.getUTCHours()),
pad(d.getUTCMinutes()),
pad(d.getUTCSeconds())].join(":") + "Z";
}
formatDate(new Date());
Output:
"2011-02-24T21:01:55Z"
This script can take care of it
/* use a function for the exact format desired... */
function ISODateString(d){
function pad(n){return n<10 ? '0'+n : n}
return d.getUTCFullYear()+'-'
+ pad(d.getUTCMonth()+1)+'-'
+ pad(d.getUTCDate())+'T'
+ pad(d.getUTCHours())+':'
+ pad(d.getUTCMinutes())+':'
+ pad(d.getUTCSeconds())+'Z'}
var d = new Date();
document.write(ISODateString(d)); // prints something like 2009-09-28T19:03:12Z