My input is
var dt = "06/01/2018"
var time = "06:25:00"
i want output to be in string like this "2018-06-01T00:55:00.000Z".
I did var result = new Date(dt+time); //output is object here
I want to convert that object to string. Can any one tell me how to do that.
There is no need to use a Date object if the dt and time formats are known in advance. Here is how you can do it
const dt = '06/01/2018';
const [mm,dd,yyyy] = dt.split('/')
const time = '06:25:00';
const date = `${yyyy}-${mm}-${dd}T${time}.000Z`;
console.log(date);
const dt = '06/01/2018';
var time = "06:25:00";
console.log(new Date(`${dt} ${time}`).toJSON())
var dt = "06/01/2018";
var time = "06:25:00";
var date_test = new Date((dt + ' ' + time)).toString();
console.log(date_test);
Related
So this is a new one to me. I've been working with this api and they returned a date in json format that looks like this
{
DateAdd: "/Date(1582936941390-0600)/"
}
not exactly sure how to convert this to a datetime like in the format below so I can actually do something with it.
2020-03-13 23:08:00
i have never seen this date format before! Thanks
Use moment.js to convert the date format
var data = {
DateAdd: "/Date(1582936941390-0600)/"
}
var datam = moment(data.DateAdd)
console.log(datam.format("YYYY-MM-DD HH:mm:ss")) // 2020-02-29 07:42:21
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.min.js"></script>
If you don't care about the timezone, you can just cut the timestamp out of that string:
const dateString = data.DateAdd;
const date = new Date(Number(dateString.match(/\d+/)[0]));
You can convert date into desired format by Javascript date object.
let addZero = (i) => {
if (i < 10) {
i = "0" + i;
}
return i;
}
let formatDate = (date) => {
let year = date.getFullYear(),
month = addZero(date.getMonth() + 1),
day = addZero(date.getDate() + 1),
hours = addZero(date.getHours() + 1),
minutes = addZero(date.getMinutes() + 1),
seconds = addZero(date.getSeconds() + 1);
let dateArr = [year, month, day];
let timeArr = [hours, minutes, seconds];
let result = dateArr.join('-').concat(" ", timeArr.join(':'));
return result;
}
let inputString = "/Date(1582936941390-0600)/";
let inputData = new Date(Number(inputString.match(/\d+/)[0]));
console.log(formatDate(inputData));
Please read more about Javascript date object
Hi i am trying to convert dd/mmm/yyyy into a binary/epoch date.
example:
06/mar/2015
into
1425600000000
i build up my date
var Buildup = day + slash + month + slash + year;
but now i dont have a clue how to do it i have tried something like this:
var formattedDays = Buildup.split(" ")[0].split("-");
var epoch = new Date(formattedDays[0], formattedDays[1] - 1, formattedDays[2]).getSeconds;
var epochStart = new Date(formattedDays[0], formattedDays[1] - 1,formattedDays[2],formattedTime[0],formattedTime[1],formattedTime[2],0).getTime()/1000;
but no look?
like this :
var d = new Date('06/03/2015');
var n = d.getTime();
Date Documentation
GetTime Documentation
In my ajax success I am getting result.Date as "/Date(-2208967200000)/". I need to check with the following date and proceed..
How to convert the "/Date(-2208967200000)/" to "01-01-1900" for below if condition?
if (result.Date != "01-01-1900") {
....
}
You can convert result.Date into you comparison date format, same as below example
var dateString = "\/Date(-2208967200000)\/".substr(6);
var currentTime = new Date(parseInt(dateString ));
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var date = day + "/" + month + "/" + year;
After doing this.. you can compare it with other date..
Reference
var jsonDate = "/Date(-2208967200000)/";
var date = new Date(parseInt(jsonDate.substr(6)));
alert(date);
The substr function takes out the "/Date(" part, and the parseInt function gets the integer and ignores the ")/" at the end. The resulting number is passed into the Date constructor.
jQuery dateFormat is a separate plugin. You need to load that explicitly using a tag.
You could use a regex to get the value between the brackets and then pass that to the Date():
var input = "/Date(-2208967200000)/";
var matches = /\(([^)]+)\)/.exec(input);
var date = new Date(parseInt(matches[1], 10));
Example fiddle
First, I refer :
Get the time difference between two datetimes
Then, it doesn't work as I'm using iso time format.
var now = '2014-12-12T09:30:00.0000000Z';
var then = '2014-12-12T11:00:00.0000000Z';
var timeDuration = moment.utc(moment(now)).diff(moment(then)).format("HH:mm:ss");
Try this:
var format = "YYYY-MM-DD HH:mm Z";
var now = '2014-12-12T09:30:00.0000000Z';
var then = '2014-12-12T11:00:00.0000000Z';
var timeDuration = moment(moment.utc(moment(then, format)).diff(moment(now, format))).format("HH:mm:ss");
print(timeDuration);
"01:30:00"
I want to calculate the difference between two dateTime, one date is submitted by user and other is current time:
user submitted time - now = difference in unix
user submitted time format is:
2014-03-26 10:52:00
Thanks for your help.
You can simply do this with getTime() which returns the number of milliseconds.
var ds = "2014-03-26 10:52:00";
var newDate = new Date(ds).getTime(); //convert string date to Date object
var currentDate = new Date().getTime();
var diff = currentDate-newDate;
console.log(diff);
Sometimes there are chance for cross browser compatibility in parsing the date string so it is better to parse it like
var ds = "2014-03-26 10:52:00";
var dateArray = ds.split(" "); // split the date and time
var ds1 = dateArray[0].split("-"); // split each parts in date
var ds2 = dateArray[1].split(":"); // split each parts in time
var newDate = new Date(ds1[0], (+ds1[1] - 1), ds1[2], ds2[0], ds2[1], ds2[2]).getTime(); //parse it
var currentDate = new Date().getTime();
var diff = currentDate - newDate;
console.log(diff); //timestamp difference
You can use MomentJS library
var user_submited_time = moment('2014-03-26 10:52:00');
var now = moment();
var value = user_submited_time - now;