I have time interval e.g. "01:30:00" as the string. Now I want to convert this string to a valid DateTime in javascript to manipulate. for example: add 1 hour.
You can use the momentjs library to do this rather easily.
var epoch = moment(str).unix();
http://momentjs.com/
Also refer
http://www.sitepoint.com/beginners-guide-to-javascript-date-and-time/
I solved my problem.
first Get current DateTime by new Date() second use .toDateString() third attach my time interval e.g. "01:30:00".
new Date().toDateString() + ' ' + "01:30:00" // Mon May 09 2016 01:30:00
Now use moment.js
var t = moment(new Date().toDateString() + ' ' + "01:30:00");
ّFinaly add for example 1 hour by moment().add()
var finalTime = t.add(60, 'minutes').format("hh:mm");
demo
var stringTimeInterval = "01:30:00";
var t = moment(new Date().toDateString() + ' ' + stringTimeInterval);
document.getElementById("demo").innerHTML = t.add(60, 'minutes').format("hh:mm:00");
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<div id="demo"></div>
Related
i am trying to convert a string into date type.i am giving the string value to new date().
but it's returning next day date instead of date which i am trying to convert.
var endDate = new Date("2017-03-23T23:59:59.000Z");
//end date value is now ------ Fri Mar 24 2017 05:29:59 GMT+0530 (India Standard Time).
Please suggest me how can get correct date in the format MM/DD/YYYY
This hack can help you,
var endDate = new Date("2017-03-23T23:59:59.000Z").toISOString();
it will give you,
"2017-03-23T23:59:59.000Z"
Further if you want to convert it to DD/MM/YYYY then you can use native javascript or lib like moment for that,
This simpile js will help to convert it to any format.
var endDate = new Date("2017-03-23T23:59:59.000Z").toISOString();
var d1 = endDate.split('T'); //spliting date from T
var d2 = d1[0].split('-'); //getting date part
console.log('yyyy/MM/dd', d2[0] + "/" + d2[1] + "/" + d2[2]) //YYYY/MM/DD
console.log("DD/MM/YYYY", d2[2] + "/" + d2[1] + "/" + d2[0])
jsfiddle link
if your time is in IST use below
var endDate = new Date("2017-03-23T23:59:59.00+0530");
If you check dates, you will see that your dates differs in 5h 30 mins, that is same as your date saying GMT +0530. Your original date has .000Z that is time zone of GMT +0.
Make sure you use same time zone when working with date.
Try using Date.UTC('your date')
JavaScript Date objects carry no timezone information. The only reason you saw a non-UTC date is that the browser chooses by default to display dates as local time in the console. If you don't care about the date object aligning with the exact instant in local time, you can use the following format function to turn it into MM/DD/YYYY format:
function format (date) {
var mm = ('0' + (date.getUTCMonth() + 1)).slice(-2)
var dd = ('0' + date.getUTCDate()).slice(-2)
var yyyy = date.getUTCFullYear()
return mm + '/' + dd + '/' + yyyy
}
var endDate = new Date("2017-03-23T23:59:59.000Z")
console.log(endDate.toISOString())
console.log(format(endDate))
(Credit to Ranj for posting an answer using Date#toISOString before mine.)
I have created the solution over here please find below link
https://www.w3schools.com/code/tryit.asp?filename=FD0YSGRMB59W
From Server I get Date in UTC format like ,
2016-04-13T02:37:13.211316121-04:00
When I use this to display using new Date(data.Created_at) I get 7 min time difference. Like as I am displaying my date in format {{my_date | date: 'h:mm a'}}, insted showing 12:05 PM, it dispalys 11:58 AM. So I tried this,
data.Created_at = new Date(Date.UTC(data.Created_at))
which returns null value. Is there any problem in my code? How should I get perfect date?
If you check syntax of Date.UTC,
Date.UTC(year, month[, day[, hour[, minute[, second[, millisecond]]]]])
It expects value in different variables and not in date string. You can split it and manually parse it.
You can try something like this:
JSFiddle
var d = "2016-04-13T02:37:13.211316121-04:00";
var date_arr = d.split(/[-|T|\.|:]/);
var o = new Date(Date.UTC(date_arr[0], date_arr[1], date_arr[2], date_arr[3], date_arr[4], date_arr[5]));
console.log(date_arr, o);
Also, it gives me 8:07 AM, considering the time is 2:37 and my timezone is +5:30.
Use it like this
Date.UTC(year,month,day,hours,minutes,seconds,millisec)
The code you are using is invalid way to handle date. You can use this code
new Date('2016-04-13T02:37:13.211316121-04:00').toISOString();
var created_at = new Date(createdAt);
var created_at_date = (created_at.getUTCMonth()+1) + "/" + created_at.getUTCDate() + "/" + created_at.getUTCFullYear() + "/" + created_at.getHours() + ":"
+ created_at.getMinutes() + ":" + created_at.getSeconds();
Hope this will work for you!!!
I try to get the correct date format, like this: 24-7-2015.
date = new Date("24-7-2015").toString({ dateFormat: 'd-M-yy' })
but the output of date is then: Wed Dec 7 00:00:00 UTC+0100 2016
Thank you
See this answer
If you want to make it easely, like in your example try to use momentjs
moment('24-7-2015', 'D-M-YYYY').format('DD-MM-YY');
Please try with the below code snippet.
var date = new Date("24-7-2015") // If this is not worked than check your local system date format
document.write(date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear());
Let me know if any concern.
Good evening,
I am writing server application that will be running on node websocket and im having hard time processing dates.
This is piece of code that i wrote:
var getDatetime = function() {
var checkLength = function(part) {
return (part < 10) ? '0' + part : part;
};
var date = new Date(),
year = date.getFullYear(),
month = checkLength(date.getMonth()),
day = checkLength(date.getDay()),
hour = checkLength(date.getHours()),
minute = checkLength(date.getMinutes()),
second = checkLength(date.getSeconds());
return day + '-' + month + '-' + year + ' ' + hour + ':' + minute + ':' + second;
};
It pains me to use it like that, im no pro with js so im asking, is there a method like in php date('d-m-Y H:i:s', time()) with which you can get current datetime in nice and clean way instead of doing this the way i showed?
I would recommend Moment.js. It can be deployed both on front end and nodejs server. Here's the install instruction for nodejs.
With the Javascript constructor, date(), under the Conversion getter section of the page linked to, there are several options for converting the format such as date.doDateString(). This will create a human-readable string, and with it being a string it can be cut up and re-arranged as needed with the use of sub strings.
var a = new Date();
console.log(a); // Wed March 25th 2015 16:10:38GMT -0700 (Pacific Daylight Time)
With AngularJS you have an easy way to show dates which are in the epoch function, with the date filter.
I have this string in Javascript
"/Date(1317772800000)/"
and I'd like to display it as a meaningful date in my page. Currently, when I output the variable that contains this date I get the following displayed on my page
/Date(1317772800000)/
What I'd like is to display this in the format DD MM YYYY like so
10 05 2011
How is this possible?
try this
var date = new Date(Number.parseFloat('/Date(1317772800000)/'.substring(6)));
var newdate = date.getMonth() +' ' +date.getDate() +' ' +date.getFullYear()
If you have your date in a string provided then first you need to extract the number:
var strDate = "/Date(1317772800000)/";
var dateInt = strDate.replace("/Date(","").replace(")/","");
var date = new Date(parseInt(dateInt))
This gives you a JavaScript date object that you can do pretty much a lot with, if you want simple check just execute:
alert(date)
Try using moment.js i.e.:
moment().format('MMMM Do YYYY, h:mm:ss a')
Then you can do:
moment(1317772800000).format("MMM Do YY");
Try this
unixtime = 1317772800000;
var newDate = new Date();
newDate.setTime(unixtime);
dateString = newDate.toUTCString();
alert(dateString);
DEMO