javascript datetime in custom format as per timezone [duplicate] - javascript

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
How to initialize a JavaScript Date to a particular time zone
(20 answers)
Closed 4 years ago.
I want datetime in custom format as per below.
2019-01-24T05:52:29:420 +0000 GMT+0000
but when I try do it using below syntax I did not work.
var currentdate = new Date();
alert(currentdate);
Any help? Thanks

Look into Moment.js, you can format JS dates with this quite easily.
e.g:
moment().format('MMMM Do YYYY, h:mm:ss a'); // January 24th 2019, 5:13:29 pm
moment().format('dddd'); // Thursday
in your case:
moment(currentdate).format('the format you would like');
https://momentjs.com/

its very simple you just have to do currentdate.toISOString() which will return "2019-01-24T06:19:19.975Z", now its simple just remove last char from string and concat the other string part in it.
var currentdate = new Date().toISOString();
alert(currentdate.substr(0, currentdate.length-1)+' +0000 GMT+0000');

Related

new Date() vs. Utilities.formatDate(new Date()) [duplicate]

This question already has answers here:
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Google Apps Script date format issue (Utilities.formatDate)
(2 answers)
Closed 2 years ago.
Can anyone tell me why this code would produce two different dates?
let now = new Date(); // today's date (1/2/2021)
Logger.log(now); // Sat Jan 02 09:42:47 GMT-08:00 2021
Logger.log(new Date(now.getTime()-(2*1000*60*60*24))); // Thu Dec 31 09:42:47 GMT-08:00 2020
Logger.log(Utilities.formatDate(new Date(now.getTime()-(2*1000*60*60*24)), "GMT-8", "MM/d/YYYY")); // 12/31/2021
Why would Utilities.formateDate() change the date from 12/31/2020 to 12/31/2021?
******** SOLUTION *********
Change the date format from "MM/d/YYYY" to "MM/d/yyyy".
It's just a simple formatting problem. Look here
This:
Logger.log(Utilities.formatDate(new Date(now.getTime()-(2*1000*60*60*24)), "GMT-8", "MM/d/YYYY"));
Should be this:
Logger.log(Utilities.formatDate(new Date(now.getTime()-(2*1000*60*60*24)), "GMT-8", "MM/d/yyyy"));
In other words just make the capital Y's lower case y's and you're there.

1/1/2000 20:00:00 only the time - Javascript [duplicate]

This question already has answers here:
Extract time from moment js object
(4 answers)
Closed 4 years ago.
I have this date and time format 1/1/2000 20:00:00 and would like to only get the time in this form 20:00 PM or 08:00 AM
How can I format it as hh:mm a to my date?
I get it this way:
var momentDate = moment($scope.workshop.hour, 'YYYY-MM-DDTHH:mm:ss');
var jsDate = momentDate.toDate();
jsDate.toLocaleString();
Try format with HH:mm A as an argument. The A argument will display capital AM or PM as appropriate. Here's an example to get the current time into that format.
var date = new Date()
var momentDate = moment(date);
momentDate.format('HH:mm A')
Updated
Updated to match OP's request.

javascript convert Date to this format '2019-01-19T19:11:00' [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
How do I format a date in JavaScript?
(68 answers)
Closed 4 years ago.
From the server I get '2019-01-19T19:11:00.000Z' I need to convert to local timezone, so that I end up with '2019-01-19T11:11:00'. My UTC offset is 8 hrs.
new Date('2019-01-19T19:11:00.000Z') produces Sat Jan 19 2019 11:11:00 GMT-0800 (Pacific Standard Time), how do I get it back to '2019-01-19T11:11:00'? Thanks
You want the date string in iso format, respecting the local time zone:
const tzoffset = (new Date()).getTimezoneOffset() * 60000;
const d = new Date('2019-01-19T19:11:00.000Z')
console.log(new Date(d - tzoffset).toISOString().split('.')[0])
console.log('2019-01-19T11:11:00')
var now = new Date();
console.log(now.toISOString().split('.')[0]);

Javascript Change timestap format [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Convert PHP Unix Timestamp to Javascript Timestamp format
(1 answer)
Closed 6 years ago.
I initialize my timestamp variable like
var current_time = new Date();
My output is in
Tue Oct 11 2016 15:09:04 GMT+0800 (Malay Peninsula Standard Time)
format. How can I change it to 2016-10-11 07:19:48pm format?
you can acheive this using moment.js lib,
just run
moment().format('YYYY-MM-DD hh:mm:SS A'); // print 2016-10-11 03:20:88 PM

How to convert Date to Other Time Zone in Javascript [duplicate]

This question already has answers here:
Convert date to another timezone in JavaScript
(34 answers)
Closed 8 years ago.
I am trying to convert Local Date/Time to other Time Zone with JavaScript.
Dates are stored in DB as UTC.
For example,
value = "2014-08-15T11:09:10Z"
var dt = new Date(value)
the output will be in my Local Timezone
Fri Aug 15 2014 18:09:10 GMT+0700 (ICT)
But how can I convert this to other Time Zone (i.e - Moscow) by using JavaScript.
You may try like this:
function myTimeZOne(value, zone) {
var f = 'YYYY/MM/DD HH:mm:ss ZZ';
return moment(value, f).tz(zone).format(f);
}
Also check moment.js
You can use JS library such as timezone-js. You could write code like this :
var format = 'YYYY/MM/DD HH:mm:ss ZZ';
var dt = new timezoneJS.Date(format , 'Europe/London');
dt.setTimezone("Asia/Jakarta");
you can also check out other JS library like:
MomentJS
DateJS

Categories