JavaScript DateFormat Conversion - javascript

I have a the locale and a date in that locale format stored in a var in JavaScript
I need to convert that locale based date string to another local format
I have
locale : en-GB / en-US / es / ko
date : dd/mm/yyyy / mm/dd/yyyy yyyy.mm.dd
the above mentioned formats are not exact
ijust mean they are different for each
not i want that date to be displayed as "August 01 2013" like this
SO Finally i need a function(fromLocale,ToLocale,dateInFromLocaleFormat) which returns Date in ToLocale format
Can anyone help me on this

Can't you just use the JavaScript function dateFormat and set the format manually when you display the date?
for example: dateFormat("date", "mmmm dd yyyy");
more detailed here http://blog.stevenlevithan.com/archives/date-time-format

Related

how to get date value as (MM-DD-YYYY) from date picker in JS

When I pick the value from the date picker input its in this format (Tue Mar 24 2020 17:00:00 GMT-0800(PST) I need to convert it to (MM-DD-YYYY)
Here is the part from my code where I get the value of the date
Reportinfo.ProjectDate = document.getElementById("date").value;
Any Ideas ? I didn't know how to implement
"toLocaleDateString()"
You can use momentjs and format the string like this:
moment(date).format('MM-DD-YYYY');
Or if that's not an option, you can use Date object and use this solution.

How to parse a date string

I have a date string with this format
2016-08-12T15:22:43.698Z
how can I parse it to obtain a resulting string that looks like
Aug 12, 2016 5:22 PM
Is there libraries/component that could facilitate such operation or shall I do it manually by coping each part of the String?
var date = new moment('2016-08-12T15:22:43.698Z');
console.log(date.format('MMM DD, YYYY h:mm A'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.js"></script>
Use momentjs, and format the moment obj as your requirement.
If the string is in the ISO standard format, which it looks like it is, you can use Date.parse() or new Date() to turn the value into a Date object. With a Date, you can call toString() or toLocaleString() to get the date formatted in local time.
If you are targeting modern JavaScript environments, Intl.DateTimeFormat provides a very complete API for formatting the date in different locales.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat

moment format verbose date as date object

I used materialize datepicker to pick a date in french format. Now I need this date formatted back to a date object so I can use it in my api. Here's how I try to convert the date back to a normal format:
moment("dimanche 30 juillet 2017","dddd D MMMM YYYY").locale('fr').toDate();
But I receive Invalid Date. Is there a way to convert this date back using moment? Or can I somehow hook to materialize component to retrieve a normal date?
You need to set the fr locale before attempting to parse french day/monthnames.
moment.locale('fr');
moment("dimanche 30 juillet 2017","dddd D MMMM YYYY").toDate();
You can parse your input string passing locale parameter, see moment(String, String, String) docs:
As of version 2.0.0, a locale key can be passed as the third parameter to moment() and moment.utc().
Here a working sample:
var m = moment("dimanche 30 juillet 2017", "dddd D MMMM YYYY", 'fr');
console.log(m.toDate());
console.log(m.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js"></script>
For further info see Changing locale globally and Changing locales locally.

Convert YYYY-MM-DD HH:MM:SS to different format in Javascript

I have a string in javascript as 2016-02-27 20:24:39 and I want to convert this as 27th Feb 08:24pm.
What is the easiest way to do in Javascript?
Checkout the JavaScript library called moment.js.
Since the default format for moment is ISO 8601 (YYYY-MM-DD HH:MM:SS), you don't need to tell moment how to parse the input String date (it defaults to ISO 8601), so you can simply write:
var now = "2016-02-27 20:24:39";
var formattedDate = moment(now).format("Do MMM HH:mma");
console.log(formattedDate);
Demo:
https://jsfiddle.net/gekd97dy/
More information about displaying in different formats can be read here:
http://momentjs.com/docs/#/displaying/
There is a non-standard Date method toLocaleFormat('%d-%b-%Y'). But appears to only work in Firefox for now.
Better use the date.format library (only 125 lines)
var date = new Date('2016-02-27 20:24:39');
dateFormat(date, "dS mmm, h:MMTT");

How to work with month function of vbscript with different local

below is the code of VBScript,
FromDate=22/2/2013
ToDate= 1/3/2013
StartDateSerial = DateSerial(year(FromDate),month(FromDate), day(FromDate))
ToDateSerial = DateSerial(year(ToDate),month(toDate),day(ToDate))
in this date format is dd/mm/yyyy. but result of DateSerial is as below
StartDateSerial= 2/22/2013
ToDateSerieal= 1/3/2013
which mean for start date it converted to mm/dd/yyyy but for ToDateSerieal it converts to dd/mm/yyyy
I found this issue on US lacal. But At UK local this is same for both.
Now when I try this with following values in FromDate and To Date
FromDate=2/22/2013
ToDate= 3/1/2013
I am passing FromDate and ToDate from JavaScript to vbscript.
Format is mm/dd/yyyy then it works fine on US lacal but not on UK local. Can any body tell me how can I fix this.
I assume, you pass d/m/y strings from Javascript to VBScript running with US locale which expects m/d/y date strings. If presented with a bad date string, VBScript tries to do the right thing and converts "22/2/2013" to a february date; "1/3/2013" is seen as a valid january date. UK locale understands d/m/y strings, "1/3/2013" is a march date.
To solve your problem - d/m/y date string input for all locales - use a = Split("d/m/y", "/") on the strings and DateSerial(a[2], a[1], a[0]).
To make sure you are working on the correct locale, you can use SetLocale to set the locale. For dd/mm/yyyy you can use 2057, the English UK locale.
cl = GetLocale()
SetLocale(1033) ' US locale
wscript.echo FormatDateTime("1/3/2013",1)
SetLocale(2057) ' UK locale
wscript.echo FormatDateTime("1/3/2013",1)
' Set back the original locale
SetLocale(cl)
' Output:
' Thursday, January 03, 2013
' 01 March 2013

Categories

Resources