Modifying an ISO Date in Javascript - javascript

I'm creating several ISO dates in a Javascript program with the following command:
var isodate = new Date().toISOString()
which returns dates in the format of "2014-05-15T16:55:56.730Z". I need to subtract 5 hours from each of these dates. The above date would then be formatted as "2014-05-15T11:55:56.730Z"
I know this is hacky but would very much appreciate a quick fix.

One solution would be to modify the date before you turn it into a string.
var date = new Date();
date.setHours(date.getHours() - 5);
// now you can get the string
var isodate = date.toISOString();
For a more complete and robust date management I recommend checking out momentjs.

do you have to subtract the hours from a string?
If not then:
var date= new Date();
date.setHours(isodate.getHours() - 5);
var isoDate = new Date().toISOString();
If you do have to use a string I'd still be tempted to do:
var date= new Date("2014-05-15T16:55:56.730Z");
date.setHours(isodate.getHours() - 5);
var isoDate = new Date().toISOString();

The way to modify Date objects is via functions such as Date.setHours, regardless of the format you then use to display the date. This should work:
var theDate = new Date();
theDate.setHours(theDate.getHours() - 5);
var isodate = theDate.toISOString();

For complex date operations and enhanced browser compatibility, I highly recommend using moment.js, especially if you're going to be doing several. Example:
var fiveHoursAgo = moment().subtract( 5, 'hours' ).toISOString();

Moment.js is perfect for this. You can do:
var date = new Date();
var newDate = moment(date).subtract('hours', 5)._d //to get the date object
var isoDate = newDate.toISOString();

Related

Javascript date formats customizing

I'm trying to test an API to check if the dates are present in the comment. The date is given in ISO format in the comment like 2020-02-18 21:30:13
When I compare with the Date() and convert it to ISO format, the format is slightly different from my date which makes my test to fail. How do I make the format the same as the one is my API response?
Below is my code:
var dateobj = new Date();
var B = dateobj.toISOString();
pm.test("Comment has Date", function (){
pm.expect(responseBody.split("*/")[0]).to.include(B)
})
Something Like This?
Javascript is not very flexible with Dates. But I think creating a formatting function shouldn't be a problem at all, try this:
var date = new Date();
var formattedDate = (date)=>{
return (`${date.getFullYear()}-${date.getMonth()}-${date.getDay()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`);
}
alert(formattedDate(date));

How to convert html5 input time to a timestamp in JavaScript?

Let's say I have 2:12 PM as my time from an input, and I want to convert it to a timestamp combined with the current date.
I can get the current timestamp using Date.now() but my problem is I want the time to be based on the input.
If moment can be used, then better. Any help would be much appreciated.
You could pass in the time format to moment's constructor, along with the input's value as string to parse it into a moment object. Like this:
console.log(moment('2:12 PM',"hh:mm a").format('lll'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
You should be able to use momentJS custom parsing:
moment("2:12 PM", "H:mm A");
Using simple JavaScript
Take date
var d = new Date();
Split it
var l = d.toString().split(":");
Slice it
var f =l[0].slice(0,-2);
Get your time variable
var ty="11:22:00";
Create Date
var j = new Date(f + ty);
Done, It's in j
One line solution:
var d = new Date();
var ty = "11:22:00";
var newDate = new Date(d.toString().split(":")[0].slice(0,-2) + ty);
It's the full date, you can use and change it as you like.

How to add a number of days to UTC now in JS?

I've read a bunch of the related questions but just can't figure this one out.
This is what i have:
var days = "10"; // input is in string
var now = new Date();
var nowUtc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
var newDate = nowUtc.setDate(nowUtc.getDate() + parseInt(days));
The values just don't seem correct so i must be doing it wrong. Can anyone help?
I want the value in the end to be ISO 8601 format.
Thanks!
setDate changes the Date object itself. you can use nowUtc.toISOString() to get ISO 8601 string.
nowUtc.setDate(nowUtc.getDate() + parseInt(days));
var iso = nowUtc.toISOString();
Just use moment.js - a library which simplifies DateTime manipulation in JavaScript.
What you are looking for is Add function and String function.
I hope it helps you!

Convert systematic date to formatted date

I want to convert systematic date into readable date format. However when I pass systematic date as argument to date constructor I get Invalid date response. How to do this properly in order to display formatted date such as dd-mm-yyyy for GMT+2 ?
var date = message.date; // => 1466663308000
var dateObject = new Date(date);
console.log(dateObject);
Console output:
Invalid Date
You have to make sure the timestamp value is a number and not a string:
var date = message.date;
var dateObject = new Date(+date); // note the +
console.log(dateObject);
Once you've got a valid date, there are many other questions here about formatting dates.
I tried the code, it is absolutely correct.
I can get the correct date
var d=new Date(1466663308000);
document.write(d);
But I tried another way:
var x = "1466663308000";
var d=new Date(x);
document.write(d);
I got the "Invalid date", so I guess, message.date should be a string, please try to long(message.date).

Converting date using .toISOString() doing nothing?

This is doing my head in and I don't know why it's happening - would love some insight.
This works fine for converting the current date and time into ISO8601 format:
var today = new Date().toISOString();
console.log(today);
However, that method fails if I alter the created date before converting it. Is it because this method must be used on date creation?
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
tomorrow.toISOString();
console.log(tomorrow);
The output will be a non converted date string for tomorrows date (the +1 simply increments the day by one, after creating the date).
For the love of god, WHY!?
toISOString() returns a String, but does not alter the original object.
Instead of doing...
...
tomorrow.toISOString();
console.log(tomorrow);
Just do
console.log(tomorrow.toISOString());
You simply log wrong thing. You should log tomorrow.ToISOString() instead of tomorrow:
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
console.log(tomorrow.toISOString());
Output:
2015-11-06T11:29:31.136Z

Categories

Resources