moment js custom date format conversion is going wrong - javascript

In my angular 4 application I am am receiving the date input in the below format:
"26-SEP-17 09.20.44.123000 AM", I need to convert this to "Sep 26, 2017 9:20:44 AM". So I have used momentjs and the code is as shown below:
var convertedDate = moment("26-SEP-17 09.20.44.123000 AM", 'DD-MMM-YY HH.mm.ss.SSS a').format('MMM d, YYYY h:mm:ss A');
console.log(convertedDate)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.js"></script>
But I am getting convertedDate as "Sep 2, 2017 9:20:44 AM". I could not able to understand why I am getting the date as Sep 2, 2017 instead Sep 26, 2017, even though the hours, min and sec are coming correctly.

Use correct format DD instead of d
var convertedDate = moment("26-SEP-17 09.20.44.123000 AM", 'DD-MMM-YY HH.mm.ss.SSS a').format('MMM DD, YYYY h:mm:ss A');
console.log(convertedDate)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.js"></script>

instead of format('MMM d, YYYY h:mm:ss A'),
make it format('MMM DD, YYYY h:mm:ss A') //notice the 'd' become 'DD'
your new code must be:
convertedDate = moment("26-SEP-17 09.20.44.123000 AM", 'DD-MMM-YY HH.mm.ss.SSS a').format('MMM DD, YYYY h:mm:ss A');

Related

How can I validate the formate of a date string that has timezone in it?

If I have a this date: April 14, 2022 14:00 UTC, how I can validate that it is in MMMM DD, YYYY HH:mm <timezone>?
I tried it with moment but it doesn't have format for timezone.
moment(date, 'MMMM DD, YYYY HH:mm',true).isValid()
How can I validate that the string is in MMMM DD, YYYY HH:mm <timezone> format?
You can include " UTC" as literal text in the parse format, e.g.
let date = 'April 14, 2022 14:00 UTC';
console.log(
moment(date, 'MMMM DD, YYYY HH:mm[ UTC]',true).isValid()
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.2/moment.min.js"></script>
Per the moment.js documentation, various offset formats are supported, so if there are other offset representations you can use multiple tests to support say "UTC" and "+00:00" (or ±HH:mm in general), e.g.
['April 14, 2022 14:00 UTC',
'April 14, 2022 14:00 +00:00',
'April 14, 2022 14:00 +05:30'
].forEach(date =>
console.log(`${date} is valid? ` +
`${moment(date, 'MMMM DD, YYYY HH:mm[ UTC]', true).isValid() ||
moment(date, 'MMMM DD, YYYY HH:mm Z', true).isValid()}`
)
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.2/moment.min.js"></script>
Unfortunately isValid doesn't support multiple formats in the one call so you can't do:
moment(date, 'MMMM DD, YYYY HH:mm[ UTC]', 'MMMM DD, YYYY HH:mm Z', true).isValid()
as only the first format is recognised.

how to do the date conversion in moment.js using javascript?

Here is my date string and i want to convert this into javascript date object.
Input: -- Wednesday, March 4th, 2020, 5:00:00 pm
Expected result: -- 2020-03-13T15:04:16.913Z'
I tried :
moment('Wednesday, March 4th, 2020, 5:00:00 pm').format('YYYY-MM-DDTHH:mm:ss\\Z')
You need to inform Moment about the format of the string you're giving it.
From the documentation / display :
Wednesday = dddd
March = MMMM
4th = Qo
2020 = YYYY
5:00:00 pm = h:mm:ss a
So yout input format is "dddd, MMMM Qo, YYYY, h:mm:ss a"
Now you can create a valid Moment and manipulate it.
moment('Wednesday, March 4th, 2020, 5:00:00 pm', "dddd, MMMM Qo, YYYY, h:mm:ss a").format(whatever)
Firstly you need to inform moment the format of the expected input , followed by format() to convert the same into required format.
Based on their docs: https://momentjs.com/docs/#/displaying/
Check this code out. It will help you get started:
var input = "Wednesday, March 4th, 2020, 5:00:00 pm";
var date = moment(input, "dddd, MMMM Do, YYYY, h:mm:SSSS a").format(
"YYYY-MM-DDTHH:mm:ss\\Z"
);
console.log(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Format the current date and time using momentjs

I want to convert the current date and time to in the following way using moment.js.
Current date and time using javascript new Date(): Thu Jul 12 2018 09:28:51 GMT+0530 (India Standard Time) and want to convert the above format to below mentioned format.
1. Thu, 12 Jul 2018 09:31:37 GMT
2. 2018-07-12T09:31:38Z
You can learn more about formatting with moment.js here.
Escape words in formatting with escaping-characters "[]".
console.log(moment());
console.log(moment().format('ddd, DD MMM YYYY HH:mm:ss [GMT]'));
console.log(moment().format('YYYY-MM-DD[T]HH:mm:ss[Z]'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
you can also try like below for
nodejs
var moment = require('moment');
var format1 = moment().utcOffset(330).format('ddd, DD MMM YYYY HH:mm:ss [GMT]')
var format2 = moment().toDate();
console.log(format1);
console.log(format2);
angular
import moment from 'moment';
var format1 = moment().utcOffset(330).format('ddd, DD MMM YYYY HH:mm:ss [GMT]')
var format2 = moment().toDate();
console.log(format1);
console.log(format2);
Install moment like below
npm install --save moment
Html javascript
var format1 = moment().utcOffset(330).format('ddd, DD MMM YYYY HH:mm:ss [GMT]')
var format2 = moment().toDate();
console.log(format1);
console.log(format2);
moment source script
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
You can use toGMTSting method to convert local time into GMT format. Hope this helps..
console.log(new Date().toGMTString());
console.log(new Date("Fri Jan 20 2012 11:51:36 GMT-0530").toGMTString());
You can try these also:
var moment = require('moment');
let startDate= new moment('11/22/1990','MM/DD/YYYY').format("YYYY/MM/DD");
You can also understand these:
let otherDate= 1399919400000;
var neweDate = moment(otherDate).format('DD/MM/YYYY');
//My neweDate output is "13/05/2014";
moment.locale('cs');
console.log(moment.locale()); // en
moment("2010-10-20 4:30", "YYYY-MM-DD HH:mm"); // parsed as 4:30 local time
moment("2010-10-20 4:30 +0000", "YYYY-MM-DD HH:mm Z"); // parsed as 4:30 UTC
You can verify the date also:
moment("not a real date").isValid(); // false
moment("2010 13", "YYYY MM").isValid(); // false (not a real month)
moment("2010 11 31", "YYYY MM DD").isValid(); // false (not a real day)
moment("2010 2 29", "YYYY MM DD").isValid(); // false (not a leap year)
moment("2010 notamonth 29", "YYYY MMM DD").isValid(); // false (not a real month name)
You can create a Moment with a pre-existing native Javascript Date object.
var day = new Date(2011, 9, 16);
var dayWrapper = moment(day);
You can create a moment with an array of numbers that mirror the parameters passed to new Date()
[year, month, day, hour, minute, second, millisecond]
moment([2010, 1, 14, 15, 25, 50, 125]); // February 14th, 3:25:50.125 PM
Any value past the year is optional, and will default to the lowest possible number.
moment([2010]); // January 1st
moment([2010, 6]); // July 1st
moment([2010, 6, 10]); // July 10th

Dynamically convert timestamp to a different timezone [duplicate]

This question already has answers here:
How to convert Moment.js date to users local timezone?
(5 answers)
Closed 6 years ago.
I am using moment.js and moment-timezone. how can I create a function to convert 2016-06-12 06:22:18 UTC time to the user's timezone time and then format the time to Jun 12, 2015 6:22 PM
for example:
updateTime("2016-06-12 06:22:18");
if in chicago, the output will be:
"Jun 12, 2016, 1:22 AM"
You can try something like this.
moment.utc("2016-06-12 06:22:18", "YYYY-MM-DD HH:mm:ss").tz('America/Chicago').format("MMM D, YYYY, hh:mm a")
Hey this links may help you
https://gist.github.com/founddrama/2182389
https://www.sitepoint.com/managing-dates-times-using-moment-js/
http://momentjs.com/timezone/docs/
Examples:
moment().format('MMMM Do YYYY, h:mm:ss a'); // June 20th 2016, 11:10:20 am
moment().format('dddd'); // Monday
moment().format("MMM Do YY"); // Jun 20th 16
moment().format('YYYY [escaped] YYYY'); // 2016 escaped 2016
moment().format();
moment.tz('2016-01-01', 'America/Chicago').format('z');
You might want to look at this answer, perhaps: Get current timestamp from specific timezone
They solved by taking the time in UTC and converting it in the specific time
var utcEpochSeconds = dateObj.getTime() + (dateObj.getTimezoneOffset() * 60000);
The function you need would be like this :
function getLocalDate (input)
{
var dt= moment.utc(input);
dt.add(moment().utcOffset(),'minutes');
return moment(dt).format('MMM D, YYYY, hh:mm a');
};
Or this :
function getLocalDate(input)
{
return moment.utc(input).local().format('MMM D, YYYY, hh:mm a');
};

Moment js convert milliseconds into date and time

I have current time in milliseconds as - 1454521239279
How do I convert it to 03 FEB 2016 and time as 11:10 PM ?
Moment parser
moment(1454521239279).format("DD MMM YYYY hh:mm a") //parse integer
moment("1454521239279", "x").format("DD MMM YYYY hh:mm a") //parse string
Moment unix method
moment.unix(1454521239279/1000).format("DD MMM YYYY hh:mm a")
If you have 1o digit 1591074937 then you can convert like this.
moment(1591074937*1000).format("DD MMM YYYY")
OR
new Date(1591074937 * 1000)
just do this
moment(1662969163000).format("DD/MM/YYYY")

Categories

Resources