I have the following timestamp: 2016-03-29T14:14:43.000Z. Is there any easy way to use JavaScript to make it look something like the following: Mar 29, 2016 2:14p? I tried using Date.parse() but it didn't seem to do anything.
{{yourValue| date:"MMM d, yyyy h:ma"}}
var ts = "2016-03-29T14:14:43.000Z";
var date = new Date(ts);
console.log(date); // Displays Tue Mar 29 2016 16:14:43 GMT+0200 (Romance Summer Time)
Is that what you need ?
var ds = date.toUTCString();
console.log(ds.substr(0,24)); // Displays Tue Mar 29 2016 16:14:43
Related
1 - Make a function that given a date in text format "dd/mm/yyyy" returns a Date object with that date, using the Split() method.
2 - You have to do a "console.log() of the Date object created" and it should output something similar to
Mon Dec 2 2019 11:36:25 GMT+0100 (Central European Standard Time).
I have tried this but I don't know why when I write a console.log
I get the following:
2022-12-05T00:00:00.000Z
And I try to get it to appear something like this:
Mon Dec 2 2019 12:30:05 GMT+0100 (Central European Standard Time)
function convertDate(date) {
let dateArray = date.split("/");
let DateString = dateArray[2] + "/" + dateArray[1] + "/" + dateArray[0] + "Z";
let dateDate = new Date(dateString);
return dateDate;
}
console.log(convertDate("05/12/2022"));
you can use new Date() to get the date like "Wed Dec 28 2022 11:36:25 GMT+0100"
let date = new Date("05/12/2022");
console.log(date);
output will be Thu May 12 2022 00:00:00 GMT+0500 (Pakistan Standard Time) {}
I get a date string as Fri Sep 17 2021 11:50:59 GMT-0400 (Eastern Daylight Time) from one place. I get it from 2021-09-17T11:50:59-04:00 in a second place.
I want to convert the first format to the second.
I am doing this in a crazy way, so I am thinking there must be a better one.
var d = new Date(`Fri Sep 17 2021 11:50:59 GMT-0400 (Eastern Daylight Time)`);
var iso = d.toISOString();
var time = d.toTimeString();
console.log(iso);
console.log(time);
var [date] = iso.split('T')
var [,localTime, timezone] = time.match(/([^ ]+) GMT([^ ]+)/);
var timezoneWithColon = timezone.replace(/(-*[0-9]{2,2})([0-9]{2,2})/,"$1:$2")
var desiredFormat = '2021-09-17T11:50:59-04:00';
var convertedFormat = `${date}T${localTime}${timezoneWithColon}`;
console.log(desiredFormat)
console.log(convertedFormat)
console.log(desiredFormat === convertedFormat);
The fiddle is over at https://jsfiddle.net/Dave_Stein/8tLv2g4j/.
2021-09-17T11:50:59-04:00 is an ISO-8601 date string.
toISOString should work, however it will convert the time to UTC. If you want this to format in your current timezone, you'll have to use a library such as date-fns:
import { formatISO } from 'date-fns'
formatISO(new Date(`Fri Sep 17 2021 11:50:59 GMT-0400 (Eastern Daylight Time)`))
// prints '2021-09-17T11:50:59-04:00'
After isolating the problem in my current project .... I think something is not working currently with the Date object in javascript.
But I don't want to be arrogant because maybe I am doing something wrong, is it a bug? or am I doning something wrong?
I want to convert a date to diffrent time zone in javascript, I made this function:
function ConvertToTimeZone(date, timezone) {
let convertedTimeString = date.toLocaleString("en-US", {hour12: false, timeZone: timezone})
return new Date(convertedTimeString)
}
Then I am using it like this:
let testDate = new Date("Thu May 21 2020 17:04:05 GMT-0700 (Pacific Daylight Time)");
let convertedDate = ConvertToTimeZone(testDate, "Europe/London");
Or like this:
let testDate = new Date("Thu May 21 2020 18:04:05 GMT-0700 (Pacific Daylight Time)");
let convertedDate = ConvertToTimeZone(testDate, "UTC");
But something wrong is happening when I use this date:
let testDate = new Date("Thu May 21 2020 17:04:05 GMT-0700 (Pacific Daylight Time)");
let convertedDate = ConvertToTimeZone(testDate, "UTC");
with that last exmaple convertedDate end up with Invalid Date {}
And when I look closely in my ConvertToTimeZone the result of date.toLocaleString(...) is 5/22/2020, 24:04:05 which looks strange because the hours of that time cannot be 24 (it should be 0)
I have this string "Mon Oct 21 2013 21:00:00 GMT-0300 (ART)" and I need to convert it to the timezone (GMT-0300) using just moment.js (not moment-timezone.js)
I'm doing this but it's returning the same hour.
var startDateTime = moment(date).format('YYYY-MM-DD HH:mm Z'),
startMoment = moment.parseZone(startDateTime).zone();
console.log(moment(startDateTime).zone(startMoment).format("YYYY-MM-DD HH:mm"));
Any help ?
To convert a moment object with timezone A to timezone B, you can do the following:
var startDateTime = "Mon Oct 21 2013 21:00:00 GMT-0300 (ART)";
var newDateTime = moment(startDateTime).zone('-0400').format('YYYY-MM-DD HH:mm')
Also note that in your example, the initial time "Mon Oct 21 2013 21:00:00 GMT-0300 (ART)" is already GMT-0300.
I get time in milliseconds from the server. I convert it to Date and get -
Mon Jul 22 2013 11:16:01 GMT+0200 (W. Europe Daylight Time) as the date in the record.
I want to separate out data of Monday, Tuesday etc into arrays. I am thinking of converting this date to Mon Jul 22 2013 23:59:59 GMT+0200 (W. Europe Daylight Time) and then filter out the records.
How can i change the date to the required end of the day time? or is there an easier way to do this ?
You could always construct a new DateTime object just using the year, month and day properties from the existing date, like so:
var actualDate = new Date(); // 2013-07-30 17:11:00
var endOfDayDate = new Date(actualDate.getFullYear()
,actualDate.getMonth()
,actualDate.getDate()
,23,59,59); // 2013-07-30 23:59:59
For future visitors, just use
var start = new Date();
var end = new Date();
start.setHours(0,0,0,0);
end.setHours(23,59,59,999);
Using http://momentjs.com:
var now = new Date().getTime();
var endOfDay = moment(now).endOf("day").toDate(); // Wed Jan 20 2016 23:59:59 GMT-0800 (PST)
var actualDate = new Date()
var eodDate = new Date(Math.floor(actualDate.getTime()/86400000+1)*86400000 + actualDate .getTimezoneOffset()*60000 - 1000)
where 86400000 are total milliseconds in a day
If two Date Objects are on the same day then they have the same Date String:
new Date('1374488161000').toDateString()
=> "Tue Jul 30 2013"
new Date('13744917610403').toDateString()
=> "Tue Jul 30 2013"
Although a rather naive method of comparing days, it's probably the simplest comparison.