This question already has answers here:
How do I format a Microsoft JSON date?
(42 answers)
Closed 9 years ago.
I need to parse date from JSON (I can't do any change in this JSON on server).
{ ...
"time":"2014-02-14 18:37:48",
...
}
In php date() it is: YYYY-mm-dd HH:ii:ss
I want to change date format, for example to "dd.mm.YYYY HH:ii". In PHP it is easy, but in JavaScript I do not know how to parse it.
I try jQuery dateFormat, but I still do an error :-(
Can you please help me?
var arr=time.split(' ');
var date_arr=arr[0];
var time_arr=arr[1];
var temp_date=date_arr.split('-');
var temp_time=time_arr.split(':');
var js_date=temp_date[2]+'.'+temp_date[1]+'.'+temp_date[0]+' '+temp_time[0]+":"+temp_time[1];
You need to do all by your hands. Javascript's Date object has enough methods.
So please try smth like this:
var dateTime = new Date(Date.parse("2014-02-14 18:37:48"));
var date = dateTime.getDate().toString().length > 1 ? dateTime.getDate() : '0' + dateTime.getDate();
var month = dateTime.getMonth().toString().length > 1 ? dateTime.getMonth() + 1 : '0' + (dateTime.getMonth() + 1);
var hours = dateTime.getHours().toString().length > 1 ? dateTime.getHours() : '0' + dateTime.getHours();
var minutes = dateTime.getMinutes().toString().length > 1 ? dateTime.getMinutes() : '0' + dateTime.getMinutes();
var formattedDate = date + '.' + month + '.' + dateTime.getFullYear() + ' ' + hours + ':' + minutes;
console.log(formattedDate);
Related
This question already has answers here:
How to convert a string to an integer in JavaScript
(32 answers)
Closed 3 years ago.
Trying to add a number new date in javascript
How ever the number is coming in from a json file.
Here is what i have.
myObj = {"yearsleft":"2", "name": "john"};
var term = myObj.yearsleft;
var d = new Date();
var year = d.getFullYear() + term.toString();
var month = d.getMonth()+1;
var day = d.getDate();
var output = ''+ (day<10 ? '0' : '') + day + '/' + (month<10 ? '0' : '') + month + '/' + year;
alert(output);
above is a working example
However its just appending 2 to the end of the year. which isnt what i want it to do.
I want it to add onto 2019
if that's possible.
You are adding string to a number, convert it to int and then add.
var year = d.getFullYear() + parseInt(term.toString(), 10);
Convert to a number - currently you're concatenating not adding.
myObj = {"yearsleft":"2", "name": "john"};
var term = myObj.yearsleft;
var d = new Date();
var year = d.getFullYear() + +term;
var month = d.getMonth()+1;
var day = d.getDate();
var output = ''+ (day<10 ? '0' : '') + day + '/' + (month<10 ? '0' : '') + month + '/' + year;
console.log(output);
d.getFullYear() + parseFloat(term);
Seem to have fixed it for me.
This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 5 years ago.
I want to bind data in a dd-mm-yy format for which I am using json to bind. Currently I get date as 2014-06-18T00:00:00
I want in dd-mm-yy format. Kindly let me know how to do that.
Below is my code for the same.
if (getJSONValue.LAUNCH_DATE != "" || getJSONValue.LAUNCH_DATE == null) {
$('#txtLaunchDate').val(getJSONValue.LAUNCH_DATE);
}
my getJSONValue.LAUNCH_DATE = 2014-06-18T00:00:00
see snippet
var newDate = new Date("2014-06-18T00:00:00");
var day = newDate.getDate();
var month = newDate.getUTCMonth() + 1;
var year = newDate.getFullYear();
console.log(day + "-" + ("0" + (month)) + "-" + year );
Using momentjs:
const date = '2014-06-18T00:00:00'
const format = 'DD-MM-YY'
console.log(moment(date).format(format))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.js"></script>
This question already has answers here:
How to ISO 8601 format a Date with Timezone Offset in JavaScript?
(21 answers)
Closed 6 years ago.
In my database I must put a date time in ISO format with time zone. For example I have the following entry column:
17/02/2016 22:00:00 +01:00
I have a web service that accept a date (in JSON object) like this:
{
//...
"Start": "2016-02-17T22:00:00+01:00"
//...
}
Now in my javascript code i've tried:
var today = new Date();
var dateString = today.toISOString();
But the output of dateString is:
"2016-03-05T12:10:32.537Z"
How can I get a string like this:
"2016-03-05T13:10:32.537+01:00"
Thanks
I believe you can't obtain a local ISO 8601 format directly from a Date function. toISOString() gives you the time in UTC / GMT: 2016-03-05T12:10:32.537Z (that's what the Z in the end is for, it means UTC)
This is how you can do it by composing the string yourself:
var date = new Date(); // this is your input date
var offsetHours = -date.getTimezoneOffset() / 60;
var offsetMinutesForDisplay = Math.abs(-date.getTimezoneOffset() % 60);
var offsetHoursForDisplay = Math.floor(offsetHours) + (offsetHours < 0 && offsetMinutesForDisplay != 0 ? 1 : 0);
var isoOffset = (offsetHours >= 0 ? ("+" + fillDigit(offsetHoursForDisplay, true)) : fillDigit(offsetHoursForDisplay, true)) + ':' + fillDigit(offsetMinutesForDisplay, true);
document.getElementById('myDiv').innerHTML = date.getFullYear() + '-' + fillDigit(date.getMonth() + 1, true) + '-' + fillDigit(date.getDate(), true) + 'T' + fillDigit(date.getHours(), true) + ':' + fillDigit(date.getMinutes(), true) + ':' + fillDigit(date.getSeconds(), true) + isoOffset;
function fillDigit(value, withDigit) { // we want to display 04:00 instead of 4:00
if (value >= 0 && value < 10) {
return (withDigit ? "0" : " ") + value;
}
if (value > -10 && value < 0) {
return '-' + (withDigit ? "0" : " ") + (-value);
}
return value;
}
<div id='myDiv'></div>
You can check out http://currentmillis.com/?now for Javascript that will get you multiple formats
If you want a custom format you need format the date by yourself using Date object methods like:
date = new Date();
hour= date.getHours();
min= date.getMinutes();
sec= date.getSeconds();
time= hour+':'+min+':'+sec;
console.log(time)
This can be encapsulated in a function or in a object method for convenience.
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 8 years ago.
I'm having a hard time trying to figure this out
$datenow = date('Y-m-j H:i:s'); // 2014-08-19 17:56:13
I would like to generate the exact date format with JS, how I could do this?
This is how it should appears: 2014-08-19 19:57:59
I would go like this :)
date = new Date();
dateFormated = date.getFullYear() + '-' + (date.getMonth()+1) + '-' +
date.getDay() + ' ' + date.getHours() + ':' + date.getMinutes() + ':'
+ date.getSeconds();
http://jsfiddle.net/r5mdggc8/2/
There are no leading zeros, but you can add them by using condition on each "date item" like so:
dateItem = getDay();
if (dateItem.toString().length < 2) {
dateItem = '0' + dateItem;
}
Of course, you can make a function out of it.
This question already has answers here:
Convert date from string in javascript
(4 answers)
Closed 3 years ago.
I have a datetime string being provided to me in the following format:
yyyy-MM-dd HH:mm:ss
2011-07-14 11:23:00
When attempting to parse it into a JavaScript date() object it fails. What is the best way to convert this into a format that JavaScript can understand?
The answers below suggest something like
var myDate = new Date('2011-07-14 11:23:00');
Which is what I was using. It appears this may be a browser issue. I've made a http://jsfiddle.net/czeBu/ for this. It works OK for me in Chrome. In Firefox 5.0.1 on OS X it returns Invalid Date.
This works everywhere including Safari 5 and Firefox 5 on OS X.
UPDATE: Fx Quantum (54) has no need for the replace, but Safari 11 is still not happy unless you convert as below
var date_test = new Date("2011-07-14 11:23:00".replace(/-/g,"/"));
console.log(date_test);
FIDDLE
One can use the getmonth and getday methods to get only the date.
Here I attach my solution:
var fullDate = new Date(); console.log(fullDate);
var twoDigitMonth = fullDate.getMonth() + "";
if (twoDigitMonth.length == 1)
twoDigitMonth = "0" + twoDigitMonth;
var twoDigitDate = fullDate.getDate() + "";
if (twoDigitDate.length == 1)
twoDigitDate = "0" + twoDigitDate;
var currentDate = twoDigitDate + "/" + twoDigitMonth + "/" + fullDate.getFullYear(); console.log(currentDate);
Just use Date.parse() which returns a Number, then use new Date() to parse it:
var thedate = new Date(Date.parse("2011-07-14 11:23:00"));
Use:
enter code var moment = require('moment')
var startDate = moment('2013-5-11 8:73:18', 'YYYY-M-DD HH:mm:ss')
Moment.js works very well. You can read more about it here.
function ConvertDateFromDiv(divTimeStr) {
//eg:-divTimeStr=18/03/2013 12:53:00
var tmstr = divTimeStr.toString().split(' '); //'21-01-2013 PM 3:20:24'
var dt = tmstr[0].split('/');
var str = dt[2] + "/" + dt[1] + "/" + dt[0] + " " + tmstr[1]; //+ " " + tmstr[1]//'2013/01/20 3:20:24 pm'
var time = new Date(str);
if (time == "Invalid Date") {
time = new Date(divTimeStr);
}
return time;
}
You can use moment.js for that, it will convert DateTime object into valid Javascript formated date:
moment(DateOfBirth).format('DD-MMM-YYYY'); // put format as you want
Output: 28-Apr-1993
Hope it will help you :)
new Date("2011-07-14 11:23:00"); works fine for me.
You can use get methods:
var fullDate = new Date();
console.log(fullDate);
var twoDigitMonth = fullDate.getMonth() + "";
if (twoDigitMonth.length == 1)
twoDigitMonth = "0" + twoDigitMonth;
var twoDigitDate = fullDate.getDate() + "";
if (twoDigitDate.length == 1)
twoDigitDate = "0" + twoDigitDate;
var currentDate = twoDigitDate + "/" + twoDigitMonth + "/" + fullDate.getFullYear(); console.log(currentDate);