Moment js convert milliseconds into date and time - javascript

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")

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>

moment js custom date format conversion is going wrong

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');

Use Moment.js to convert Unix epoch time to human readable time

I'm trying to use Moment.js to convert a Unix epoch time to a date and time. I'd also like to know how to have it formatted like below.
Tuesday, November 22, 2016 6:00 PM
moment.unix(yourUnixEpochTime).format('dddd, MMMM Do, YYYY h:mm:ss A')
From the Docs: Unix Timestamp
var day = moment.unix(1318781876); //seconds
var day = moment(1318781876406); //milliseconds
// and then:
console.log(day.format('dddd MMMM Do YYYY, h:mm:ss a'));
// "Sunday October 16th 2011, 9:17:56 am"
You can use .format('LLLL') for your requirement.
let result = moment(epoch).format('LLLL');
let epoch = 1562127342123;
let result = moment(epoch).format('LLLL');
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js" integrity="sha256-4iQZ6BVL4qNKlQ27TExEhBN1HFPvAvAMbFavKKosSWQ=" crossorigin="anonymous"></script>
You can use moment.unix(epochTime).
Moment JS converts the Unix time to GMT equivalent. To convert it to EST which is 5 hours behind Coordinated Universal Time (UTC) :
[momentjs]
const moment = require('moment');
console.log(moment(1580331903396).subtract(5, 'hours').format('MMMM Do, YYYY - h:mm:ss A '))
Refer working example here:
https://repl.it/repls/CumbersomeImmediateLibrary

Inbuild Functionality to Format Dates using Jquery

I have a requirement to show a date in the format as below in the label control.
Wednesday 20 Jul 2016 11:31 AM
Is there any in build functionality available with Jquery without righting my on function to build the string in the specific way as above?
You can use date.format library:
var dateFormat = require('dateformat');
var now = new Date();
dateFormat(now, "dddd dS mmmm yyyy h:MM TT");
It will return
Wednesday 20 Jul 2016 11:31 AM
Use the dateFormat library by Steven Levithan. It's just 1.2kb when minified and gzipped.
Here's how to use it:
var _date = dateFormat(new Date(), "dddd dd mmm yyyy hh:MM TT"); //use _date variable to display
You can also use this JQuery library : Date Format
var now = new Date();
document.write($.format.date(now, "ddd dd MMM yyyy hh:mm a"));

Categories

Resources