Javascript Moment.js Change time from AM to PM - javascript

I am trying to add 12:00 PM to an existing Moment date object. In debugging, the date object looks like this
Tues Sept 01 2019 00:00:00 GMT-0400
I am convert to a string but getting the AM after the conversion.
MyDate = moment(this.TestDate.format("MM/DD/YYYY h:mm A");
I read the moment.js docs and thought that adding the 'A' would change the AM to PM but so far it does not work. I read a few post and tried a few different version of code but so far no luck.
I would like to get the following date string after the conversion
10/10/2019 12:00 PM
Thanks

Adding 'A' in format() only display 'AM'/'PM'. You need to first manipulate your date and then display the new value.
Assuming this.TestDate is a moment instance :
MyDate = moment(this.TestDate.add('12', 'hours').format("MM/DD/YYYY h:mm A");
About manipulation
Be careful about moment's manipulation, it change the moment instance inside your variable, meaning :
MyDate = moment('Tues Sept 01 2019 00:00:00 GMT-0400')
console.log(Mydate); // Show Tues Sept 01 2019 00:00:00 GMT-0400
MyDate2 = MyDate.add(12, 'hours');
console.log(Mydate); // Show Tues Sept 01 2019 12:00:00 GMT-0400

Related

moment timezone js returns wrong datetime

I am using Moment Timezone to get current datetime for Asia/Tokyo timezone.
The code is as following
var currentDateTime = new Date(moment().tz('Asia/Tokyo').format('YYYY-MM-DDTHH:mm:ss'))
Supposed that the current datetime for my local timezone is as following
Thu Jul 22 2021 19:49:47 GMT+0700 (Indochina Time)
I expected the current date for Asia/Tokyo timezone would be as following
Thu Jul 22 2021 21:49:47 GMT+0700 (Indochina Time)
In Chrome I got the expected datetime.
But in Safari on my iPhone, I got the wrong datetime.
Fri Jul 23 2021 04:49:47 GMT+0700 (Indochina Time)
It seems the current datetime returned is the right current datetime plus 7 hours.
Here is my environment
iPhone : iPhone 6
iOS : 12.5.4
The problem you have is caused by the Date constructor parsing the time according to the local timezone (in Chrome) and UTC (on Safari?).
For the time given, this code
moment().tz('Asia/Tokyo').format('YYYY-MM-DDTHH:mm:ss')
returns
"2021-07-22T21:49:47"
You then pass this time into the Date constructor:
var currentDateTime = new Date("2021-07-22T21:49:47")
Because this string has no time zone information, it is assumed to be in the device's local timezone (Chrome) or UTC (Safari) which gives the wrong date object.
While you could inject the timezone into this string (using ZZ), you can build a Date object from the moment object using:
var currentDateTime = moment().tz('Asia/Tokyo').toDate()
but this is effectively the same as
var currentDateTime = new Date()
If the intended output is a string of the format:
"Thu Jul 22 2021 21:49:47 GMT+0900 (Japan Standard Time)"
the closest you could get is:
moment().tz('Asia/Tokyo').format('ddd MMM D YYYY, h:mm:ss a [GMT]ZZ (z)')
// Thu Jul 22 2021 21:49:47 GMT+0900 (JST)
Alternatively, there is also the localized "Month name, day of month, day of week, year, time" format:
moment().tz('Asia/Tokyo').format('llll (z)')
// In my locale, I get:
// Thu, Jul 22, 2021 9:49 PM (JST)
Take a look at the Moment display format documentation for more options.

How to convert time to specific string?

I'm working with a date that looks like this:
Mon Feb 04 2019 15:57:02 GMT-0700 (Mountain Standard Time)
and I'm trying to convert it to this:
2019-02-04T15:57:02.000Z
but for some reason my code always adds 7 hours and ends up being like this:
"2019-02-05T22:57:02.000Z"
Can anyone tell me what I'm doing wrong? Thanks a lot in advance!
Here's my code:
new Date(myTime as string).toISOString();
I'd use Moment.js, which is a decent date parsing and formatting library. To get what you're looking for, you'd use a statement like:
console.log(moment
.parseZone(
"Mon Feb 04 2019 15:57:02 GMT-0700 (Mountain Standard Time)",
"ddd MMM DD YYYY HH:mm:ss 'GMT'ZZ") // the format of the string presented
.local()
.format('YYYY-MM-DDTHH:mm:ss')); // the format of the output
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
I've broken the single line out into parts so it's a bit easier to read. A few notes:
parseZone allows you to parse the "-0700" from the string.
local converts the date from the parsed time zone to the current time zone
format formats the date.
The format topic has a list of the formatting tokens used.
The Parse > String + Format topic lists the parsing tokens (which are the same as the formatting tokens for the most part).
Note that the output does not have a "Z" at the end; this is important because without the "Z", it is a local date. With the "Z" you are are actually specifying a date and time that is 7 hours earlier than the one you've been given.
I'm not sure how to get this as a one-liner, but this is one way:
var time = new Date('Mon Feb 04 2019 15:57:02 GMT-0700 (Mountain Standard Time)')
new Date(time.setHours(time.getHours() + 7)).toISOString()
"2019-02-05T12:57:02.000Z"
Your code is not adding hours to the input date. What is happening is that your date string is using a particular timezone GMT-0700 (Mountain Standard Time) and the time zone used in new Date().toISOString() is UTC GMT+0000 (UTC). So when in the Mountain Standard Time timezone is Mon Feb 04 2019 15:57:02, in the UTC timezone is actually 2019-02-05T22:57:02.000Z. There are your seven hours from GMT-0700 to GMT+0000.
EDITED
If you don't really care about time zones and want to obtain 2019-02-04T15:57:02.000Z from Mon Feb 04 2019 15:57:02 GMT-0700 (Mountain Standard Time) you could just strip everything after GMT to let new Date() think it is an UTC date.
var timeString = 'Mon Feb 04 2019 15:57:02 GMT-0700 (Mountain Standard Time)';
new Date(timeString.substr(0, timeString.indexOf('GMT') + 3));
2019-02-04T15:57:02.000Z

Transpose date into local timezone without shifting the date

I have an input date with a specific time zone in string. For example:
Sat May 20 2017 17:00:00 GMT-0300 (-03)
I want to change the timezone of this date to my local timezone without converting the time. As if the input date was in my correct timezone.
Sat May 20 2017 17:00:00 GMT+0200 (CEST)
I played around with moment and found this:
const date = moment(moment.utc('Sat May 20 2017 17:00:00 GMT-0300 (-03)').format('LLL'))
Is it the simplest way to do it? I don't like the copy of the moment object but I can't find a method to do it in a "single shot".
To parse your input in local time, you can simply use moment(String, String) function. As docs says:
By default, moment parses and displays in local time.
Here a live sample:
var input = 'Sat May 20 2017 17:00:00 GMT-0300 (-03)';
var m = moment(input, 'ddd MMM DD YYYY HH:mm:ss');
console.log(m.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Date format - Logic error

I have a date saved to the var mydate. When i print that i get the string equivalent of Tue Feb 10 2009 00:00:00 GMT-0800 (Pacific Standard Time).
Now i need to format the var mydate so it will display the date as Tue Feb 10 2009.
The code;
var today = $(this).datepicker('getDate');
var mydate = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay());
When i display my date, i got the string equivalent of Tue Feb 10 2009 00:00:00 GMT-0800 (Pacific Standard Time). But what i want is this Tue Feb 10 2009. How can i do this by formatting var my date ?
Have you tried myDate.toDateString()?
Here's an example:
> new Date().toDateString()
"Fri Feb 22 2013"
Also have a look at moment.js.
From your code I am assuming you are using the jQuery date picker from http://www.kelvinluck.com.
Assuming you are, and you are using the Date class provided with the package you can do something like this:
//Date.format = 'dd/mm/yyyy';
Date.format = 'D M dd yyyy';
//Tue Feb 10 2009
$('.date-picker').datePicker({
startDate:'01/01/2001'
//other options here..
});
One way that you could achieve this is by creating a custom date (by creating a new class called myDate which extends Date) and override the .toString() method. That it returns the information in the format you desire.
There are good and simple libraries to format dates easily.
You can try this one:
http://www.mattkruse.com/javascript/date/

JS date object is not parsing correctly

We currently save the date in this format: 12/12/2011 8:00:00 PM.
When I parse it in JS Date object, it converts into this: Tue, 13 Dec 2011 00:00:00 GMT.
I do not believe this is a timezone issue because we are -4 GMT and it does not add up. Any ideas?
var start = new Date("12/12/2011 8:00:00 PM");
alert("12/12/2011 8:00:00 PM");
alert(start.toUTCString());
It seems to add up perfectly. "12/12/2011 8:00:00 PM" in GMT-4 is exactly "Tue, 13 Dec 2011 00:00:00 GMT".
EDIT
Use toLocaleString to get the date and time in your local time zone.
start.toLocaleString()

Categories